GenBigStack:
CEDAR
PROGRAM
IMPORTS DragOpsCrossUtils, HandCoding, HandCodingSupport, HandCodingPseudos
= BEGIN OPEN DragonProcessOffsets, HandCoding, HandCodingSupport, HandCodingPseudos;
StackedStatusWord: TYPE = DragOpsCross.StackedStatusWord;
Word: TYPE = DragOpsCross.Word;
useCST:
BOOL ←
FALSE;
For the current SoftCard the CST instruction does not really work. We can avoid it on single-processor systems by fancy coding.
bogusPadBits: NAT ← 2;
markPadBits: NAT ← 1;
bogusStatus: StackedStatusWord = [
padBits: bogusPadBits, userMode:
FALSE, trapsEnabled:
FALSE];
Size parameters (the only difference between GenStack.mesa and GenBigStack.mesa)
initialFrames:
NAT = 5*256;
Initial frames to allocate.
localFrameArraySize:
NAT = 2*16;
# of frames in localArray
framesToTransfer:
NAT = localFrameArraySize/2;
Number of frames to transfer between local and global frame pools.
(this # should be explored)
StackSize:
NAT = 128;
StackLog: NAT = BITS[[0..StackSize)];
Note: StackSize = 2^StackLog
statusMask: StackedStatusWord = [userMode:
TRUE, trapsEnabled:
TRUE, lBase: StackSize-1];
The mask for the "interesting" bits in the stacked status word.
statusMask0: StackedStatusWord = [userMode:
TRUE, trapsEnabled:
TRUE, lBase: 0];
The mask for the "interesting" bits in the stacked status word without the saved L.
regsPerNacho: NAT = 16;
RegArray: TYPE = ARRAY [0..regsPerNacho) OF Word;
Nacho: TYPE = LONG POINTER TO NachoRep;
NachoRep:
TYPE =
RECORD [
link: Nacho, -- link to previous frame
lastPC: Word, -- continuation PC for this frame
nRegs: Word, -- number of regs used for this frame at last save
others: Nacho, -- if # NIL, then it is a pointer to an aux frame
regs: RegArray -- addressable by local regs
];
linkOffset:
NAT = 0;
constLink: ConstSpec = const0;
lastPCOffset:
NAT = 1;
constLastPC: ConstSpec = const1;
nRegsOffset:
NAT = 2;
constNRegs: ConstSpec = const2;
othersOffset:
NAT = 3;
constOthers: ConstSpec = const3;
regOff: CARDINAL = 4; -- offset of DumpBlock.regs
LocalAllocationRep:
TYPE =
RECORD [
lockPtr: GlobalAllocationPtr, -- pointer to global lock for frame allocation
ptrs: LocalArray -- start of frame pointers
];
LocalArray:
TYPE =
ARRAY [0..localFrameArraySize]
OF Nacho;
type of holder for frames per processor, limit is quite arbitrary (need not be fixed)
NIL is stored at the very end to make test for empty easy
GlobalAllocationPtr: TYPE = LONG POINTER TO GlobalAllocationRep;
GlobalAllocationRep:
TYPE =
RECORD [
lock: Word, -- global spin lock for frame allocation
gFree: Word, -- pointer to the next free frame in frames
emergency: Word, -- pointer to the emergency limit for frames
frames: GlobalArray -- space for the global pool of nachos
];
GlobalArray:
TYPE =
ARRAY [0..initialFrames+1]
OF Nacho;
type of holder for frames, limit is quite arbitrary (need not be fixed)
NIL is stored at the very end to make test for empty easy
StackMargin:
NAT ← 17;
Number of words in EU stack to reserve to handle stack overflow. Will this really be enough? This has hardware implications as well (see Don Curry)
Global labels
stackUnderflowTrap: Label ←
NIL;
Entry point for handling stack underflow (aka DragonStack.StackUnderflowTrap). Also used as the PC for the bogus frame.
internalSaveEldest: Label ←
NIL;
Entry point for the internal routine (aka DragonStack.InternalSaveEldest) that saves the eldest frame.
globalFreeNacho: Label ←
NIL;
Entry point for the internal routine that frees up a nacho to the central pool.
globalAllocNacho: Label ←
NIL;
Entry point for the internal routine that allocates up a nacho to the central pool.
StackUnderflowTrap:
PROC [stackUnderflowTrap: Label] = {
This code is called when we return to the bogus frame, which has an invalid PC. At entry, maskable traps are disabled, and the IFU stack has exactly 1 frame (if the world is valid). The top of stack has no words that we can depend on. The idea is to just bring in one frame from the hook, and then "return" to it, enabling traps as we go.
area: HandCodingSupport.Area ← HandCodingSupport.GetCurrentArea[];
dispatchData: Label = GenDataLabel[area, StackSize*DragOpsCross.bytesPerWord];
WordAlign[];
{
At the underflow point we have no frames on the IFU stack, provided that we are in kernel mode. If not in kernel mode, then we will soon trap. No code in user mode can fake this entry in kernel mode, so we don't have to check. At this point the stack may contain return values from the procedure that just returned to us.
underflowEntry1: Label = GenLabelHere[];
This is an internal routine, which is the only way we can set L. At this point we have one frame on the IFU stack, which will vanish on return. However, we need to calculate the new value for L, then set L to that value. We assume that the hook register points at a valid nacho!
[S] = nRegs field of the restored frame (with status bits)
GetYoungestStatus[];
drRVSUB[topDst, topSrc, belowSrc];
ExtractField[first: 32-StackLog, bits: StackLog];
drDUP[];
[S] = [S-1] = the new value for L, properly masked
[S-2] = nRegs field of the restored frame
SetYoungestStatus[];
Set the new youngest L, which will set L on return.
[S] = the new value for L, not masked
[S-1] holds the nRegs field of the restored frame
Calculate & set the new stack limit.
drDUP[];
[S] = [S-1] = new L (properly masked)
[S-2] = nRegs field of the restored frame
drSUBB[StackMargin];
ExtractField[first: 32-StackLog, bits: StackLog];
SetSPLimit[];
LRegI[hook, constLastPC];
[S] = return PC for restored frame
[S-1] = new L (masked)
[S-2] = nRegs field of the restored frame.
drRETN[];
WordAlign[];
SetLabel[stackUnderflowTrap];
MakeLabelGlobal["
DragonStack.StackUnderflowTrap", stackUnderflowTrap];
This is the bogus frame PC when the bogus frame is created using the stack overflow code. The saved status will have traps disabled and be in kernel mode, and the saved L value will be the current L.
LRegI[hook, constNRegs];
[S] = nRegs field of the restored frame
drLIQB[LOOPHOLE[statusMask]];
drAND[];
[S] = nRegs field & status bits for the restored frame
drLFC[UseLabel16[underflowEntry1]];
This call sets L and [S] to the new L.
[S] = return PC for restored frame
[S-1] = new L (masked)
[S-2] = nRegs field of the restored frame.
SetEldestPC[];
Creates the frame that will receive the restored information & sets the return PC.
[S] = new L (masked)
[S-1] = nRegs field of the restored frame (with status bits, masked)
drQOR[pushA0, belowSrc];
drQOR[pushA0, belowSrc];
drSHDR[DragOpsCrossUtils.FieldDescriptorToCard[[insert:
TRUE, mask: StackLog]]];
[S] = new L & status bits for the restored frame
[S-1] = new L (masked)
[S-2] = nRegs field of the restored frame
SetEldestStatus[];
Sets the proper status for the restored frame.
Create the bogus frame
[S] = new L (no status bits)
[S-1] = nRegs field of the restored frame
PushByteAddr[area, stackUnderflowTrap];
SetEldestPC[];
Create the bogus frame & set the PC
drADDQB[
LOOPHOLE[bogusStatus]];
Set the bogus bits to mark the bogus frame
SetEldestStatus[];
the bogus frame is now correct
ExtractField[first: 32-StackLog, bits: StackLog];
[S] = #regs (no status bits) for the restored frame
At this point we have two frames on the IFU stack, both of which are completely correct. L is correctly set as well to the base of the frame to be restored. The hook register is pointing at the nacho chain to be used to restore the frame.
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
[S] = dispatch PC
[S-1] = #regs (no status bits)
MoveReg[temp, hook];
temp has the nacho to chase
MoveRegI[hook, hook, constLink];
hook now has the next frame's head nacho
drJSD[];
Dispatch to save the proper # of regs; on arrival:
[S] = #regs (no status bits)
};
{
This section contains the various routines for restoring varying numbers of registers and the table for dispatching to those routines.
singleLabel: Label = GenLabel[];
multiLabel: Label = GenLabel[];
fatalLabel: Label = GenLabel[];
SetLabel[singleLabel];
At this point we execute a varying number of RAIs to restore the regs
[S] = #regs (no status bits)
FOR i:
NAT
DECREASING
IN [0..regsPerNacho)
DO
drRAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
MoveReg[topDst, temp]; FreeNacho[];
free the nacho pointed to by temp (& flush the # regs)
MakeLabelGlobal["
DragonStack.StackUnderflowReturn", GenLabelHere[]];
Now S is back at its initial value, hook is properly set
drRETN[];
Return from kernel, no change to S.
SetLabel[fatalLabel];
We should never have this happen! Control only gets here for a truly smashed nacho!
Pause[];
SetLabel[multiLabel];
Now do regsPerNacho reads into the stack registers from the nacho.
FOR i:
NAT
IN [0..regsPerNacho)
DO
drRAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
LReg[temp]; MoveRegI[temp, temp, constOthers];
push the nacho to free; point hook at the next nacho in the chain
[S] = nextNacho, [S-1] = #regs
FreeNacho[];
free the nacho pointed to by [S]
[S] = # regs
drSUBB[regsPerNacho];
adjust #regs
[S] = #regs left
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
drAL[regsPerNacho];
adjust L to account for the registers restored
drJSD[];
Dispatch to save the proper # of regs
[S] = # of regs to save
{
Generate the dispatch table
oldPC: CARD ← HandCodingSupport.GetOutputPC[];
newPC: CARD ← dispatchData.offset;
HandCodingSupport.SetOutputPC[newPC];
FOR nr:
NAT
DECREASING
IN [0..regsPerNacho]
DO
OutputByteAddr[area, singleLabel, 3*nr];
ENDLOOP;
FOR nr:
NAT
IN (regsPerNacho..regsPerNacho*4]
DO
OutputByteAddr[area, multiLabel, 0];
ENDLOOP;
FOR nr:
NAT
IN (4*regsPerNacho..StackSize)
DO
OutputByteAddr[area, fatalLabel, 0];
ENDLOOP;
HandCodingSupport.SetOutputPC[oldPC];
};
};
};
StackOverflowTrap:
PROC [underflowLabel: Label] = {
Note: we assume that there is sufficient reserve space on the EU stack and the IFU stack to complete the saving of one frame. Traps are automatically disabled on entry to this routine, and must be reenabled on the way out.
area: HandCodingSupport.Area ← HandCodingSupport.GetCurrentArea[];
internalSaveSetup: Label ← GenLabel[];
exitLabel: Label ← GenLabel[];
dispatchLabel: Label = GenLabel[];
dispatchData: Label = GenDataLabel[area, StackSize*DragOpsCross.bytesPerWord];
{
DragonStack.IFUStackOverflowTrap: This part is for IFU stack overflow. Note that we are NOT assured of a non-empty frame on the IFU stack.
ifuEntryLabel: Label = GenLabel[];
ProcedureEntry[ifuEntryLabel, 0,
TRUE];
We get no arguments.
MakeLabelGlobal["DragonStack.IFUStackOverflowTrap", ifuEntryLabel];
FillTrap[IFUStackOverflowTrap, ifuEntryLabel];
GetEldestPC[];
discard the bogus frame
[S] holds the PC for the bogus frame
drRUADD[topDst, const0, const0];
Capture the carry bit
[S] holds the carry bit
GetEldestStatus[];
[S] holds the eldest status, [S-1] holds the carry bit
AllocNacho[];
Allocate the next save block to use
([S] = newBlock, [S-1] = eldest status)
drLFC[UseLabel16[internalSaveSetup]];
Init the new save block. Its address is left in hook (and in temp).
L gets set to the L for the frame we want to save.
[S] = #regs, [S-1] = eldest status
SetLabel[dispatchLabel];
We get here to do the dispatch to the appropriate register saving code.
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
drSFC[];
Dispatch to restore the proper # of regs
On return: [S] = eldest status
This is the common exit stuff for the stack overflow handlers. The idea is to keep the stack limit register set with enough reserve space to be able to prevent wraparound when we execute code where traps are disabled. Also, the saved L for the bogus frame must be consistent with the number of registers in the youngest saved frame (referenced by hook).
SetLabel[exitLabel];
MakeLabelGlobal["
DragonStack.StackOverflowExit", exitLabel];
At this point: [S] holds the eldest status, [S-1] holds the carry bit
PushByteAddr[area, underflowLabel];
SetEldestPC[];
Install the new bogus frame & set its PC (creating a new eldest frame)
ExtractField[first: 32-StackLog, bits: StackLog]; drDUP[];
drADDQB[LOOPHOLE[bogusStatus]];
SetEldestStatus[];
Calculate & set the L for the bogus frame. [S] still holds that L.
drSUBB[StackMargin];
ExtractField[first: 32-StackLog, bits: StackLog];
SetSPLimit[];
Calculate & set the new stack limit. S is now back to its original depth.
[S] holds the carry bit
MakeLabelGlobal["DragonStack.StackOverflowReturn", GenLabelHere[]];
drRUSUB[topDst, const0, popSrc];
Set the carry bit that was saved on the overflow
drRETN[];
At this point we go back to the code that overflowed.
};
{
DragonStack.EUStackOverflowTrap: If we save a frame, but don't get any more space in the EU stack, then we have to keep saving frames until a frame is saved that has at least one EU register saved with it. We know that there is a non-empty frame on the register stack, otherwise why would we get an EU stack overflow?
euEntryLabel: Label = GenLabel[];
ProcedureEntry[euEntryLabel, 0,
TRUE];
We get no arguments.
MakeLabelGlobal["DragonStack.EUStackOverflowTrap", euEntryLabel];
FillTrap[EUStackOverflowTrap, euEntryLabel];
GetEldestPC[];
discard the bogus frame
[S] holds the PC for the bogus frame
drRUADD[topDst, const0, const0];
Capture the carry bit
[S] holds the carry bit
GetEldestStatus[];
[S] holds the eldest status, [S-1] holds the carry bit
{
loopLabel: Label = GenLabelHere[];
[S] = eldest status
AllocNacho[];
Allocate the next save block to use
([S] = newBlock, [S-1] = eldest status)
drLFC[UseLabel16[internalSaveSetup]];
Init the new save block. Its address is left in hook (and in temp).
L gets set to the L for the frame we want to save.
[S] = #regs, [S-1] = eldest status
drRJNEBJ[left: topSrc, right: const0, dist: UseLabel8B[dispatchLabel]];
If we have registers to save, then go do them.
drRJEBJ[popSrc, const0, UseLabel8B[loopLabel]];
Discard [S] and go loop (unconditionally, really).
[S] = eldest status
};
};
{
InstallBogusFrame is useful for external callers of DragonStack.InternalSaveEldest. Once the necessary frames have been saved, the bogus frame can be created using this routine.
exitEntryLabel: Label = GenLabel[];
ProcedureEntry[exitEntryLabel, 0, TRUE];
drRUADD[pushDst, const0, const0];
Capture the carry bit
GetEldestStatus[];
push the eldest status
[S] holds the eldest status, [S-1] holds the carry bit
MakeLabelGlobal["DragonStack.InstallBogusFrame", exitEntryLabel];
drJB[UseLabel8A[exitLabel]];
};
{
RemoveBogusFrame is useful for external callers of DragonStack.InternalSaveEldest. Before InternalSaveEldest can be called, the bogus frame must be removed, and the carry bit & current eldest status must be pushed.
exitEntryLabel: Label = GenLabel[];
ProcedureEntry[exitEntryLabel, 0];
MakeLabelGlobal["DragonStack.RemoveBogusFrame", exitEntryLabel];
GetEldestPC[];
discard the bogus frame
[S] holds the PC for the bogus frame
drRUADD[topDst, const0, const0];
Capture the carry bit
[S] holds the carry bit
GetEldestStatus[];
[S] holds the eldest status, [S-1] holds the carry bit
ProcedureExit[1];
return the new eldest status
};
{
This is an internal routine that we use to setup stuff (especially L) prior to saving the frame. It needs to be a procedure because we have to set L (sigh).
before entry:
hook = frame chain
[S] = newBlock, [S-1] = eldest status
after exit:
L = L of frame to save
[S] = #regs, [S-1] = eldest status
eldestStatus: RegSpec = reg0;
localHook: RegSpec = reg1;
localStatus: RegSpec = reg2;
ProcedureEntry[internalSaveSetup, 2];
Make our local L good for a few locals, we get one sent to us on the stack
LReg[hook]; drPSB[linkOffset];
store the hook into the new dump block
[S] = localHook = newBlock; (localHook+linkOffset)^ = hook
PReg[hook]; PReg[temp];
store the new dump block addr into hook and temp
[S] = localHook = hook = temp = newBlock
LReg[eldestStatus];
ExtractField[first: 32-StackLog, bits: StackLog];
SetYoungestStatus[];
Get the L for the frame we wish to save, put it into localStatus and youngest L
[S] = localStatus = L of frame to save; [S-1] = localHook, [S-2] = eldest status
(note that the status bits must be clear in [S])
GetEldestPC[]; drSRIn[localHook, lastPCOffset];
Store the continuation PC and remove the eldest PC from the ifuStack.
((localHook+lastPCOffset)^ = PC of frame to save)
GetEldestStatus[];
Sample & save the new eldest status
[S] (aka localStatus) = eldest status (the new version)
[S-1] = localHook
[S-2] = eldest status (the old version)
drRVSUB[c: pushDst, a: localStatus, b: eldestStatus];
Calculate the number of regs in this frame
[S] = # of regs to save (with garbage in top bits)
[S-1] (aka localStatus) = status
[S-2] = localHook
[S-3] = eldest status
MoveReg[eldestStatus, localStatus];
[S] = # of regs to save (with garbage in top bits)
[S-1] (aka localStatus) = eldest status (the new version)
[S-2] = localHook
[S-3] = eldest status (the new version)
drSHDR[DragOpsCrossUtils.FieldDescriptorToCard[[insert:
TRUE, mask: StackLog]]];
insert # of regs into the saved status word (aka localStatus)
[S] (aka localStatus) = #regs (+ status bits)
[S-1] (aka localHook)
[S-2] = eldest status
drWRI[localStatus, localHook, nRegsOffset];
Save #regs into the save block & into eldestStatus
(localHook+nRegsOffset)^ ← localStatus
[S] = #regs, [S-1] = localHook, [S-2] = eldest status
drSHDR[DragOpsCrossUtils.FieldDescriptorToCard[[mask: StackLog]]];
[S] = #regs, [S-1] = eldest status
ProcedureExit[2];
return the values
[S] = #regs, [S-1] = eldest status
};
{
DragonStack.InternalSaveEldest: This is the entry point for internally saving the eldest stack frame in the IFU stack. The bogus frame has been removed. [S] holds the sampled eldest status.
singleLabel: Label = GenLabel[];
multiLabel: Label = GenLabel[];
fatalLabel: Label = GenLabel[];
shortLabel: Label = GenLabel[];
SetLabel[internalSaveEldest];
MakeLabelGlobal["DragonStack.InternalSaveEldest", internalSaveEldest];
AllocNacho[];
Allocate the next save block to use
([S] = newBlock, [S-1] = eldest status)
drLFC[UseLabel16[internalSaveSetup]];
Init the new save block. Its address is left in hook. L gets set to the L for the frame we want to save.
[S] = #regs, [S-1] = eldest status
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
drJSD[];
Dispatch to restore the proper # of regs
after JSD: [S] = #regs, [S-1] = eldest status
SetLabel[singleLabel];
FOR i:
NAT
DECREASING
IN [0..regsPerNacho)
DO
drWAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
drDIS[];
Put the stack back to the original level
[S] = eldest status
drRETN[];
This return does not enable traps, and does not disturb S, but does restore L, which was previously clobbered. S must have the same value that it did at entry!
[S] = eldest status
SetLabel[fatalLabel];
For now we just crash. Eventually we should be more robust, saving away the regs, then giving the offending process a "stack fault".
Pause[];
SetLabel[multiLabel];
FOR i:
NAT
IN [0..regsPerNacho)
DO
drWAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
LReg[temp];
Push the address of the completed save block
([S] = temp = oldBlock; [S-1] = #regs; [S-2] = eldest status)
AllocNacho[];
Allocate the next save block to use (it gets left on the stack)
([S] = newBlock; [S-1] = temp = oldBlock; [S-2] = #regs)
PReg[temp]; drWSB[othersOffset];
set temp to the new dump block addr
also store the new block addr into the old block
(temp = (temp'+othersOffset)^ = newBlock; [S] = #regs)
drSUBB[regsPerNacho];
adjust #regs
[S] = #regs, [S-1] = eldest status
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
drAL[regsPerNacho];
adjust L for the restored regs
drJSD[];
Dispatch to restore the proper # of regs
[S] = #regs, [S-1] = eldest status
{
Generate the dispatch table
oldPC: CARD ← HandCodingSupport.GetOutputPC[];
HandCodingSupport.SetOutputPC[dispatchData.offset];
FOR nr:
NAT
DECREASING
IN [0..regsPerNacho]
DO
OutputByteAddr[area, singleLabel, 3*nr];
ENDLOOP;
FOR nr:
NAT
IN (regsPerNacho..regsPerNacho*4]
DO
OutputByteAddr[area, multiLabel, 0];
ENDLOOP;
FOR nr:
NAT
IN (4*regsPerNacho..StackSize)
DO
OutputByteAddr[area, fatalLabel, 0];
ENDLOOP;
HandCodingSupport.SetOutputPC[oldPC];
};
};
};
AllocNacho:
PROC = {
Allocate a nacho using the local array of nachos, defaulting to the global array of nachos if not immediately successful. The address of the allocated nacho is left on the stack. There is no provision for failure from the global array at this time.
exitLabel: Label = GenLabel[];
enterLabel: Label = GenLabelHere[];
LRegI[free, const0];
Push the next nacho address. If none in our local cache, a 0 will be pushed.
drLC0[];
push 0 for comparison (this cycle is free due to the preceding fetch)
drRJNEBJ[left: topSrc, right: belowSrc, dist: UseLabel8B[exitLabel]];
Jump if we got the nacho; leave 0 on stack if not. Predict success.
drLFC[UseLabel16[globalAllocNacho]];
Call to do this the expensive way (S←S-2)
drJB[UseLabel8A[enterLabel]];
Go to retry the allocation from the start (stack is back to ground level).
SetLabel[exitLabel];
This is the successful exit point. The allocated nacho is on the stack.
AddReg[free, const1];
Adjust the free pointer, since we got a nacho
[S] = 0, [S-1] = new nacho
drPSB[othersOffset];
always clears the others field on allocation
[S] = new nacho
};
GlobalAllocNacho:
PROC = {
Replenish the local nacho array from the global nacho array. There is no provision for failure from the global array at this time.
At entry, [S] = [S-1] = 0, at exit S←S-2, but the local array has nachos. AllocNacho is the only caller of this routine!
exitLabel: Label = GenLabel[];
globalAllocNacho ← GenLabelHere[];
MakeLabelGlobal["DragonStack.GlobalAllocNacho", globalAllocNacho];
drRRX[c: belowDst, a: base, b: popSrc];
Replace [S-1] (was 0) with @lock; also S←S-1
([S] = @lock)
LReg[process];
Push the desired new value for the lock field
([S] = process; [S-1] = @lock)
IF useCST
THEN {
drLC0[];
Push the assumed old value of the lock field (i.e. free)
([S] = 0; [S-1] = process; [S-2] = @lock)
{retryLabel: Label = GenLabelHere[];
drCST[0];
Try for the global lock for the nacho allocator. We store our process ID into the lock if the lock was free, and return the sampled value of the lock at the start of the instruction in any case.
([S] = sample; [S-1] = 0; [S-2] = process; [S-3] = @lock)
drRJNEB[left: popSrc, right: belowSrc, dist: UseLabel8B[retryLabel]];
Jump back to retry if we did not get it (predict success), and pop the sampled value. If this loop causes us to hold the bus too often, we can always insert a spin loop that does not use CST.
([S] = 0; [S-1] = process; [S-2] = @lock)
drAS[256-2];
On success, flush the old & new values, since we don't need them.
([S] = @lock)
};
}
ELSE {
No locking is OK for the single-processor SoftCard.
drPSB[0];
};
drRSB[1];
Get the global free pointer (gFree) on the stack
(@base.gFree = @base.lock + 1)
([S] = gFree; [S-1] = @lock)
drLIB[framesToTransfer]; drRVSUB[free, free, popSrc];
Adjust the free pointer to accomodate the next transfers.
FOR i:
NAT
IN [0..framesToTransfer)
DO
drRSB[i]; LReg[free]; drWB[i];
(free+i)^ ← (gFree+i)^
ENDLOOP;
drADDB[framesToTransfer];
adjust gFree for the number of nachos allocated
([S] = new gFree; [S-1] = @lock)
drPSB[1];
Write gFree back, leaving the addr of the lock on the stack.
([S] = @lock)
drLC0[]; drWSB[0];
Clear out the lock, which lets other processors get their chance.
(stack is now at original level)
drRETN[];
};
FreeNacho:
PROC [] = {
Free a nacho (in [S]) using the local array of nachos, defaulting to the global array of nachos if not immediately successful. There is no provision for failure from the global array at this time.
exitLabel: Label = GenLabel[];
enterLabel: Label = GenLabelHere[];
drRVSUB[c: pushDst, a: free, b: const1];
[S] = free-1; [S-1] = nacho to free
drRJNEBJ[left: topSrc, right: base, dist: UseLabel8B[exitLabel]];
Determine if free-1 will collide with base, jump if not. Free-1 is left on the stack for use in storing away the freed frame. Predict success.
drLFC[UseLabel16[globalFreeNacho]];
In the hard case we call a routine to munch on the global table.
SetLabel[exitLabel];
At this point the address of the slot to hold the nacho is on the stack. First we store that address to free, then we load the nacho to be freed and store it into the slot.
PReg[free]; drWB[0];
(free ← [S])^ ← [S-1]; S←S-2
};
GlobalFreeNacho:
PROC = {
Free up a nacho (in [S-1]) to the central pool. We have already tested for the fast case, and we call this when [S] = free-1; [S-1] = nacho to free. At exit, [S] = new free, [S-1] = nacho to free.
Determine if free-1 will collide with base, jump if not. Free-1 is left on the stack for use in storing away the freed frame. Predict success.
globalFreeNacho ← GenLabelHere[];
MakeLabelGlobal["DragonStack.GlobalFreeNacho", globalFreeNacho];
drRRX[c: topDst, a: base, b: const0];
Put the addr of the lock on the stack. We get to have a pop for free here.
([S] = @lock)
LReg[process];
Push the desired new value for the lock field
([S] = process; [S-1] = @lock)
IF useCST
THEN {
drLC0[];
Push the assumed old value of the lock field (i.e. free)
([S] = 0; [S-1] = process; [S-2] = @lock)
{retryLabel: Label = GenLabelHere[];
drCST[0];
Try for the global lock for the nacho allocator. We store our process ID into the lock if the lock was free, and return the sampled value of the lock at the start of the instruction in any case.
([S] = sample; [S-1] = 0; [S-2] = process; [S-3] = @lock)
drRJNEB[left: popSrc, right: belowSrc, dist: UseLabel8B[retryLabel]];
Jump back to retry if we did not get it (predict success), and pop the sampled value. If this loop causes us to hold the bus too often, we can always insert a spin loop that does not use CST.
([S] = 0; [S-1] = process; [S-2] = @lock)
drAS[256-2];
On success, flush the old & new values, since we don't need them. The lock address is left on the stack.
([S] = @lock)
}
}
ELSE {
No locking is OK for the single-processor SoftCard.
drPSB[0];
};
drRSB[1];
get the global free pointer (gFree) on the stack (leaving the lock addr under it)
([S] = gFree; [S-1] = @lock)
drSUBB[framesToTransfer];
gFree ← gFree - framesToTransfer; leave gFree on stack
([S] = new gFree; [S-1] = @lock)
FOR i:
NAT
IN [0..framesToTransfer)
DO
LReg[free]; drRB[i]; drPSB[i];
(gFree+i)^ ← (free+i)^
ENDLOOP;
drPSB[1];
write gFree back, leaving the lock addr on the stack
([S] = @lock)
drLC0[]; drWSB[0];
clear out the lock, which lets other processors get theirs
(stack is now at original level)
drLIB[framesToTransfer-1]; drRVADD[c: topDst, a: topSrc, b: free];
([S] = free + (framesToTransfer-1))
This is the successful exit point. There is room between free and base.
drRETN[];
};
GenFramesInit:
PROC = {
allocator: Label = HandCodingPseudos.GetGlobalLabel["Basics.AllocVector"];
rZero: RegSpec = reg0;
rLB: RegSpec = reg1; -- base of local frame table
rSB: RegSpec = reg2; -- base of shared frame table
tPtr: RegSpec = reg3; -- pointer to next slot in global table
drLC0[]; -- init rZero
drLIB[SIZE[LocalAllocationRep]/2];
drLFC[UseLabel16[allocator]]; drROR[base, topSrc, const0];
allocate the per processor frame base (LocalAllocationRep)
also put that frame into base and leave in rLB
drLIDB[framesToTransfer*2+initialFrames];
drLFC[UseLabel16[allocator]];
drWRI[rSB, rLB, 0];
allocate space for the shared frame base (GlobalAllocationRep)
also store that address into rLB.lockPtr and leave it in rSB
drWRI[rZero, rSB, 0]; drWRI[rZero, rSB, 2];
clear out the shared frame base
drRVADD[pushDst, rSB, const3]; drWRI[tPtr, rSB, 1];
init rSB.gFree and tPtr
{
Allocate the new frames from the basic system allocator. Each frame gets stuck into the shared frame base (GlobalAllocationRep) in a new slot.
loopLabel: Label = GenLabelHere[];
drLIB[20]; drLFC[UseLabel16[allocator]]; drSRIn[tPtr, 0];
Allocate and store the new frame
drRVADD[tPtr, tPtr, const1];
drLRn[tPtr]; drSUBDB[initialFrames];
drRJNEBJ[left: popSrc, right: rSB, dist: UseLabel8B[loopLabel]];
stop when tPtr gets far enough from the base
};
drLIB[17]; drRADD[free, base, popSrc]; drWRI[rZero, rLB, 17];
now make free point at the next local slot to fill
also, clear out that slot
drAS[256-4];
Finally, drop the junk from the stack
};
All:
PROC = {
area: HandCodingSupport.Area ← HandCodingSupport.GetCurrentArea[];
continueLabel: Label = GenLabel[];
toUserLabel: Label = GenLabel[];
underflowLabel: Label = GenLabel[];
internalSaveEldest ← GenLabel[];
Entry point for the internal routine (aka DragonStack.InternalSaveEldest) that saves the eldest frame.
GenFramesInit[];
drLFC[UseLabel16[continueLabel]];
Order of generation is important!
GlobalAllocNacho[];
GlobalFreeNacho[];
StackUnderflowTrap[underflowLabel];
StackOverflowTrap[underflowLabel];
SetLabel[continueLabel];
Setup the bogus frame & enable traps (to detect stack overflow).
PushWordAddr[area, underflowLabel];
SetYoungestPC[];
GetYoungestStatus[];
drLIQB[LOOPHOLE[DragOpsCross.StackedStatusWord[trapsEnabled: TRUE]]];
drOR[];
drDFC[UseLabel32[HandCodingPseudos.GetGlobalLabel["Basics.SetStatus"]]];
drLFC[UseLabel16[toUserLabel]];
This is to ensure that there is a non-bogus frame at the bottom of the stack, which is more-or-less required for stack save/restore. Someday we may want to remove this restriction. RRA: April 21, 1987
Pause[];
SetLabel[toUserLabel];
drLC0[];
MakeLabelGlobal["DragonStack.ExitToUser", toUserLabel];
};
FillTrap:
PROC [tx: DragOpsCross.TrapIndex, dest: Label] = {
area: HandCodingSupport.Area = HandCodingSupport.GetCurrentArea[];
oldPC: CARD = HandCodingSupport.GetOutputPC[area];
SetOutputPC[DragOpsCrossUtils.TrapIndexToBytePC[tx]];
drJDB[UseLabel16[dest]];
HandCodingSupport.SetOutputPC[oldPC];
};
END.