Aidan Steele
Aidan Steele

Reputation: 11330

What is this stack-checking PPC assembly doing?

I have the following self-contained function (i.e. it is branched to using bl CheckStackFunc) and I am mystified as to its purpose. Could someone familiar with PowerPC assembly lend a hand?

_CheckStackFunc:
    neg     %r11, %r12
    addi    %r0, %r11, 0xFFF
    srawi.  %r0, %r0, 0xC
    blelr
    mr      %r11, %sp
    mtctr   %r0
loc_10176B0C:                           
    lwzu    %r0, -0x1000(%r11)
    bdnz    loc_10176B0C
    blr

That having been said, I am using this document by IBM for my PPC assembly reference. Is this considered the definitive source or are there others I should be aware of?

Upvotes: 2

Views: 800

Answers (1)

ruslik
ruslik

Reputation: 14870

I'm not familiar at all with PPC, but here is my guess:

It looks like alloca_probe() to me. The code that touches the stack with steps of a page so that it would trigger the PAGE_GUARD exceptions. (please excuse my x86 language :) )

It is used after a large stack allocation (that can also be done by alloca). The stack usually don't have all the memory reserved for stack allocated, the last actually loaded page have a special flag, PAGE_GUARD, that triggers a hardware exception that is caught by OS, so that it could commit more pages to the stack. When the stack is used normally (with push/pop), then this page cannot be bypassed. However, for larger allocs, a call to alloca_probe() is required before memory can be safely used.

Upvotes: 4

Related Questions