ARM.Objects.Mod 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. MODULE Objects; (** AUTHOR "pjm"; PURPOSE "Active object runtime support"; *)
  2. IMPORT SYSTEM, Trace, Machine, Heaps, Modules;
  3. CONST
  4. (** Process flags *)
  5. Restart* = 0; (* Restart/Destroy process on exception (hardcoded in compiler (OPC.CallRecBody / PCC.SysStart)) *)
  6. PleaseHalt* = 10; (* Process requested to Halt itself soon *)
  7. Unbreakable*= 11; (* FINALLY shall not catch HALT exception (PleaseHalt is also set) *)
  8. SelfTermination*=12; (* Indicates the process has requested to terminate ifself (PleaseHalt is also set) *)
  9. Preempted* = 27; (* Has been preempted. *)
  10. Resistant* = 28; (* Can only be destroyed by itself *)
  11. (** Process modes *)
  12. Unknown* = 0; Ready* = 1; Running* = 2; AwaitingLock* = 3;
  13. AwaitingCond* = 4; AwaitingEvent* = 5; Suspended* = 6; (* Suspened for compatibility with WinAos, not used for native A2 *)
  14. Terminated* = 7;
  15. (** Process priorities *)
  16. MinPriority = 0; (* only system idle processes run at this priority level *)
  17. Low* = 1; Normal* = 2; High* = 3; (* "user" priorities *)
  18. GCPriority* = 4; (* priority of garbage collector *)
  19. Realtime* = 5; (* reserved for interrupt handling and realtime apps, these processes are not allowed to allocate memory *)
  20. NumPriorities = Heaps.NumPriorities; (* number of priority levels *)
  21. (* Process termination halt codes *)
  22. halt* = 2222;
  23. haltUnbreakable* = 2223;
  24. MinIRQ = Machine.IRQ0;
  25. NumIRQ = Machine.MaxIRQ-MinIRQ+1;
  26. Stats* = FALSE; (* maintain statistical counters *)
  27. TraceVerbose = FALSE; (* write out verbose trace info *)
  28. StrongChecks = TRUE; (* strong sanity checks *)
  29. VeryConservative = FALSE; (* temp - be very conservative about stack-based pointers *)
  30. YieldTrick = FALSE; (* avoid yield when no ready process available *)
  31. HandlePriorityInv = TRUE; (* enables or disables priority inversion handling. Handling of priority inversion leads to a simplified locking, see Lock, Unlock and Await *)
  32. (* constant used in GC Process.FindPointers *)
  33. InitDiff = MAX(LONGINT);
  34. AddressSize = SIZEOF(ADDRESS);
  35. ReturnStackDisplacement = 2 * AddressSize;
  36. TYPE
  37. CpuCyclesArray* = ARRAY Machine.MaxCPU OF HUGEINT;
  38. EventHandler* = PROCEDURE {DELEGATE};
  39. Timer* = POINTER TO RECORD
  40. next*, prev* : Timer;
  41. trigger: LONGINT;
  42. handler: EventHandler
  43. END;
  44. ProtectedObject = POINTER TO RECORD END; (* protected object *)
  45. ProcessQueue = Heaps.ProcessQueue;
  46. Body = PROCEDURE (self: ProtectedObject);
  47. Condition = PROCEDURE (slink: ADDRESS): BOOLEAN;
  48. InterruptList = POINTER TO RECORD
  49. next: InterruptList;
  50. handler: EventHandler
  51. END;
  52. TYPE
  53. (** All exported fields and variables should be considered read-only. *)
  54. Process* = OBJECT (Heaps.ProcessLink)
  55. VAR
  56. rootedNext : Process; (** for rootedProcesses *)
  57. obj-: ProtectedObject; (** associated active object *)
  58. state-: Machine.State; (** processor state of suspended process *)
  59. sse: Machine.NEONState; (* fpu and sse state of preempted process (only valid if Preempted IN flag) *)
  60. sseAdr: ADDRESS;
  61. condition-: Condition; (** awaited process' condition *)
  62. condFP-: ADDRESS; (** awaited process' condition's context *)
  63. mode-: LONGINT; (** process state *) (* only changed inside Objects lock ??? *)
  64. procID-: LONGINT; (** processor ID where running *)
  65. waitingOn-: ProtectedObject; (** obj this process is waiting on (for lock or condition) *)
  66. id-: LONGINT; (** unique process ID for tracing *)
  67. flags*: SET; (** process flags *)
  68. priority-, staticPriority*: LONGINT; (** process dynamic priority (can change during priority inversion handling) and static priority *) (* exported for AosExceptions *)
  69. stack*: Machine.Stack; (** user-level stack of process *)
  70. restartPC-: ADDRESS; (** entry point of body, for SAFE exception recovery *)
  71. restartSP-: ADDRESS; (** stack level at start of body, for SAFE exception recovery *)
  72. exp*: Machine.ExceptionState;
  73. oldReturnPC: ADDRESS;
  74. cpuCycles-, lastCpuCycles- : CpuCyclesArray;
  75. prioRequests : ARRAY NumPriorities OF LONGINT; (* priorities of processes that wait for resources locked by this process, only the highest priority per resource is stored *)
  76. context: ANY;
  77. (* set priority of process: Machine.Objects lock is taken *)
  78. PROCEDURE SetPriority(p : LONGINT);
  79. BEGIN
  80. DEC(prioRequests[staticPriority]);
  81. staticPriority := p;
  82. INC(prioRequests[staticPriority]);
  83. priority := MaxPrio(prioRequests)
  84. END SetPriority;
  85. PROCEDURE FindRoots; (* override *)
  86. VAR pc, bp, curbp, sp: ADDRESS; d0, d1: SIZE; first : BOOLEAN;
  87. BEGIN
  88. IF traceProcess # NIL THEN traceProcess(SELF) END;
  89. (* stack garbage collection *)
  90. IF (priority >= Low) & (priority <= High) & (mode >= Ready) & (mode # Terminated) THEN
  91. (* only processes with priority < GCPriority are preempted during GC,
  92. only those are allowed to allocate memory and their stacks are inspected.
  93. Furthermore, the process must be in a valid state, e.g. terminated processes have a disposed stack. *)
  94. IF Heaps.GCType = Heaps.HeuristicStackInspectionGC THEN
  95. IF VeryConservative THEN
  96. Heaps.RegisterCandidates(stack.adr, stack.high-stack.adr)
  97. ELSE
  98. sp := state.SP; (* cf. Enter *)
  99. IF sp # 0 THEN
  100. IF Machine.ValidStack(stack, sp) THEN
  101. Heaps.RegisterCandidates(sp, stack.high - sp)
  102. END
  103. ELSE
  104. Trace.String("[Objects.FindRoots sp=0]")
  105. END
  106. END;
  107. IF TraceProcessHook # NIL THEN
  108. bp := state.BP; pc := state.PC; sp := state.SP;
  109. TraceProcessHook(SELF,pc,bp,sp,stack.high);
  110. END;
  111. ELSIF Heaps.GCType = Heaps.MetaDataForStackGC THEN
  112. bp := state.BP; pc := state.PC; first := TRUE;
  113. IF pc # 0 THEN (* process is running already *)
  114. WHILE (bp # Heaps.NilVal) & (stack.adr <= bp) & (bp < stack.high) DO
  115. FindPointers(bp, pc, d0, d1);
  116. IF first THEN
  117. IF (d0 = 0) OR (d0 = 1) OR (d1 = 3) THEN
  118. (* situation where pc and bp are not synchronized: *)
  119. (* entry protocol of a procedure:
  120. PUSH EBP -- 1 byte instruction length, if pc points to this instruction at offset 0 from the codeoffset then bp still refers to caller frame -> critical
  121. MOV EBP, ESP -- 2 bytes instruction length, do. for offset 1 from the codeoffset
  122. (followed by initialization of local variables)
  123. exit protocol of a procedure:
  124. MOV ESP, EBP -- 2 bytes instruction length
  125. POP EBP -- 1 byte instruction length
  126. RET n -- 3 bytes instruction length, if pc points to this instruction at offset 3 from the last statement then bp already refers to caller's frame -> critical
  127. *)
  128. IF (d0 = 0) OR (d1 = 3) THEN
  129. SYSTEM.GET(state.SP, pc); (* matching pc is at position of stack pointer *)
  130. ELSE
  131. SYSTEM.GET(state.SP+AddressSize, pc); (* matching pc is at 4 bytes after stack pointer, pushed base pointer is at stack pointer position *)
  132. END;
  133. ELSE
  134. (* regular case: bp and pc were synchronized *)
  135. curbp := bp;
  136. SYSTEM.GET(curbp, bp);
  137. SYSTEM.GET(curbp+AddressSize, pc);
  138. END;
  139. first := FALSE;
  140. ELSE
  141. (* regular case: bp and pc were synchronized *)
  142. curbp := bp;
  143. SYSTEM.GET(curbp, bp);
  144. SYSTEM.GET(curbp+AddressSize, pc);
  145. END
  146. END
  147. END
  148. ELSE
  149. HALT(900) (* wrong GCType constant *)
  150. END
  151. END
  152. END FindRoots;
  153. (*!TODO: adapt the code according to the new Modules/Reflection *)
  154. PROCEDURE FindPointers(bp, pc : ADDRESS; VAR diff0, diff1: SIZE);
  155. (*VAR data: Modules.ProcTableEntry; startIndex, i: LONGINT; ptr : ADDRESS; success: BOOLEAN;
  156. BEGIN
  157. diff0 := InitDiff; diff1 := InitDiff;
  158. (*Modules.FindProc(pc, data, startIndex, success);*)
  159. IF success THEN
  160. diff0 := pc - data.pcFrom;
  161. diff1 := pc - data.pcStatementEnd;
  162. IF (data.noPtr > 0) & (pc >= data.pcStatementBegin) & (pc <= data.pcStatementEnd) THEN
  163. FOR i := 0 TO data.noPtr - 1 DO
  164. SYSTEM.GET(bp + Modules.ptrOffsets[startIndex + i], ptr);
  165. IF ptr # Heaps.NilVal THEN
  166. Heaps.Mark(SYSTEM.VAL(ANY, ptr))
  167. END
  168. END
  169. END
  170. END*)
  171. END FindPointers;
  172. END Process;
  173. TraceProcess* = PROCEDURE (p: Process);
  174. ExceptionHandler* = PROCEDURE(p: Process; VAR int: Machine.State; VAR exc: Machine.ExceptionState; VAR return: BOOLEAN);
  175. Idle = OBJECT
  176. BEGIN {ACTIVE, SAFE, PRIORITY(-1)} (* negative priority equivalent to MinPriority *)
  177. LOOP
  178. REPEAT
  179. IF ProcessorHLT # NIL THEN ProcessorHLT (* UP *)
  180. ELSE
  181. Machine.SpinHint; (* MP *)
  182. END;
  183. UNTIL maxReady >= lowestAllowedPriority;
  184. Yield
  185. END
  186. END Idle;
  187. Clock = OBJECT
  188. VAR h: Timer;
  189. BEGIN {ACTIVE, SAFE, PRIORITY(High)}
  190. LOOP
  191. Machine.Acquire(Machine.Objects);
  192. LOOP
  193. h := event.next;
  194. IF (h = event) OR (h.trigger - Machine.ticks > 0) THEN EXIT END;
  195. event.next := h.next; event.next.prev := event; (* unlink *)
  196. h.next := NIL; h.prev := NIL;
  197. Machine.Release(Machine.Objects);
  198. h.handler; (* assume handler will return promptly *)
  199. Machine.Acquire(Machine.Objects)
  200. END;
  201. ASSERT(timer = NIL); (* temp strong check *)
  202. timer := running[Machine.ID ()];
  203. timer.mode := AwaitingEvent;
  204. SwitchToNew
  205. END
  206. END Clock;
  207. ReadyProcesses = OBJECT(Heaps.RootObject)
  208. VAR q {UNTRACED}: ARRAY NumPriorities OF ProcessQueue;
  209. PROCEDURE &Init;
  210. VAR i: LONGINT;
  211. BEGIN
  212. FOR i := 0 TO NumPriorities - 1 DO
  213. q[i].head := NIL; q[i].tail := NIL
  214. END
  215. END Init;
  216. PROCEDURE FindRoots; (* override *)
  217. VAR i: LONGINT;
  218. BEGIN
  219. (* only mark queues of user processes since these will not change during GC *)
  220. FOR i := Low TO High DO
  221. Heaps.Mark(q[i].head);
  222. Heaps.Mark(q[i].tail)
  223. END
  224. END FindRoots;
  225. END ReadyProcesses;
  226. GCStatusExt = OBJECT(Heaps.GCStatus)
  227. VAR gcOngoing: BOOLEAN;
  228. PROCEDURE &Init;
  229. BEGIN
  230. gcOngoing := FALSE;
  231. END Init;
  232. (* called from Heaps.InvokeGC, i.e. this is a hidden upcall. However, it is necessary to take the Machine.Objects lock here since writing
  233. the set of variables here must not be interrupted, i.e. atomic writing of the set of variables is absolutely necessary. They system may hang
  234. if the lock is not taken. *)
  235. PROCEDURE SetgcOngoing(value: BOOLEAN);
  236. VAR p: Process;
  237. BEGIN
  238. IF value THEN
  239. Machine.Acquire(Machine.Objects);
  240. IF ~gcOngoing THEN
  241. gcOngoing := TRUE;
  242. lowestAllowedPriority := GCPriority;
  243. gcBarrier := Machine.allProcessors
  244. END;
  245. p := running[Machine.ID()];
  246. Enter(p);
  247. p.mode := Ready;
  248. SwitchToNew (* this method cannot schedule the running user process with priority Low, Normal or High since
  249. lowestAllowedPriority is set to GCPriority *)
  250. ELSE
  251. Machine.Acquire(Machine.Objects);
  252. gcOngoing := FALSE;
  253. lowestAllowedPriority := Low;
  254. Machine.Release(Machine.Objects)
  255. END;
  256. END SetgcOngoing;
  257. (* caller must hold Machine.Objects lock *)
  258. PROCEDURE GetgcOngoing(): BOOLEAN;
  259. BEGIN
  260. RETURN gcOngoing
  261. END GetgcOngoing;
  262. END GCStatusExt;
  263. GCActivity = OBJECT
  264. BEGIN {ACTIVE, SAFE, PRIORITY(GCPriority)}
  265. UpdateState;
  266. LOOP
  267. Machine.Acquire(Machine.Objects);
  268. ASSERT(gcProcess = NIL); (* temp strong check *)
  269. gcProcess := running[Machine.ID()];
  270. gcProcess.mode := AwaitingEvent;
  271. SwitchToNew; (* SwitchTo called by SwitchToNew will release the lock Machine.Objects *)
  272. (* process is scheduled -> gcProcess = NIL set by scheduler (Timeslice), perform garbage collection now *)
  273. Heaps.CollectGarbage(Modules.root);
  274. Machine.Acquire(Machine.Objects);
  275. IF finalizerProcess # NIL THEN
  276. (* it is safe to move finalizerProcess to the ready queue and set the variable to NIL
  277. since the process has been marked by the GC already - marking is finished here *)
  278. Enter(finalizerProcess);
  279. finalizerProcess := NIL
  280. END;
  281. Machine.Release(Machine.Objects);
  282. Heaps.gcStatus.SetgcOngoing(FALSE)
  283. END
  284. END GCActivity;
  285. FinalizerCaller = OBJECT (* separate active object that calls finalizers *)
  286. VAR n: Heaps.FinalizerNode;
  287. BEGIN {ACTIVE, SAFE, PRIORITY(High)}
  288. LOOP
  289. Machine.Acquire(Machine.Objects);
  290. ASSERT(finalizerProcess = NIL); (* temp strong check *)
  291. finalizerProcess := running[Machine.ID()];
  292. finalizerProcess.mode := AwaitingEvent;
  293. SwitchToNew; (* SwitchTo called by SwitchToNew will release the lock Machine.Objects *)
  294. (* process is scheduled -> finalizerProcess = NIL set by GCActivity, perform finalization now *)
  295. LOOP
  296. n := Heaps.GetFinalizer();
  297. IF n = NIL THEN EXIT END;
  298. IF n.collection # NIL THEN
  299. n.collection.RemoveAll(n.objStrong) (* remove it if it is not removed yet *)
  300. END;
  301. IF n.finalizer # NIL THEN
  302. n.finalizer(n.objStrong) (* may acquire locks *)
  303. END
  304. END;
  305. END
  306. END FinalizerCaller;
  307. Interrupter = OBJECT (ProtectedObject) (* to do: like Timer *)
  308. VAR interruptNumber: LONGINT;
  309. END Interrupter;
  310. VAR
  311. ready: ReadyProcesses; (* ready queue represented as an object that contains the queues *)
  312. maxReady: LONGINT; (* for all i : MinPriority <= maxReady < i < NumPriorities : Empty(ready.q[i]) *)
  313. lowestAllowedPriority: LONGINT; (* denotes the minimal user or realtime priority greater than the idle priority that can be
  314. scheduled depending on the GC status, minPriority = Low if GC is not running,
  315. minPrioriy = GCPriority otherwise *)
  316. running-{UNTRACED}: ARRAY Machine.MaxCPU OF Process; (** processes currently running, exported for Traps, not traced by the GC since it may change during collection *)
  317. nextProcessID: LONGINT;
  318. gcBarrier: SET; (* barrier that must be passed by all processors before actual collection starts *)
  319. gcActivity: GCActivity; (* active object for GC handling *)
  320. gcProcess: Process; (* suspended GC process, is NIL when collection has started, not equal NIL when no garbage collection is in process, same behaviour as for timer *)
  321. finalizerProcess: Process; (* finalizer process, regarded as part of GC *)
  322. interrupt: ARRAY NumIRQ OF RECORD
  323. root: InterruptList;
  324. process: Process
  325. END;
  326. processingIRQ: ARRAY NumIRQ OF BOOLEAN;
  327. rootedProcesses: ARRAY NumPriorities OF Process; (* list of potential processes that are not traced by GC when processing the ready queues, since GC only traces processes with
  328. priorities Low ... High in ready queues. The potentially not traced processes are rooted here and traced by the GC *)
  329. event: Timer; (* list of events *)
  330. timer (*, realtimeTimer *): Process; (* suspended timer processes *)
  331. terminate: PROCEDURE;
  332. trap, trapReturn: ARRAY 2 OF PROCEDURE;
  333. ProcessorHLT*: PROCEDURE; (** installable procedure to halt the current processor while idle *)
  334. traceProcess*: TraceProcess; (** for debugging purposes (see Info.Active) *)
  335. entry: ADDRESS;
  336. init: Process;
  337. initObject: ProtectedObject; (* Active object for the init process *)
  338. (* Performance monitoring *)
  339. idlecount*: ARRAY Machine.MaxCPU OF LONGINT; (** count of idle process timeslice interrupts *)
  340. idleCycles- : ARRAY Machine.MaxCPU OF HUGEINT; (** CPU cycles of idle threads *)
  341. perfTsc-: ARRAY Machine.MaxCPU OF HUGEINT;
  342. (* Statistics *)
  343. Nlock-, Nunlock-, Nawait-, NawaitNoIF-, NawaitTrue-, Ncreate-, Nterminate-,
  344. Ncondition-, Ncondition1True-, Ncondition2-, Ncondition2True-,
  345. Ntimeslice-, NtimesliceTaken-, NtimesliceNothing-, NtimesliceIdle-,
  346. NtimesliceKernel-, NtimesliceV86-, NtimesliceCritical-,
  347. Npreempt-, NpreemptTaken-, NpreemptNothing-,
  348. NpreemptKernel-, NpreemptV86-, NpreemptCritical-,
  349. Nenter- : LONGINT;
  350. debugCounter: LONGINT;
  351. PROCEDURE GetMaxPrio(VAR queue: ProcessQueue; VAR new: Process);
  352. VAR
  353. t: Heaps.ProcessLink;
  354. maxPriority : LONGINT;
  355. BEGIN
  356. ASSERT(new = NIL);
  357. t := queue.head;
  358. maxPriority := MIN(LONGINT);
  359. WHILE (t # NIL) DO
  360. IF (t(Process).priority > maxPriority) THEN
  361. new := t(Process); maxPriority := t(Process).priority;
  362. END;
  363. t := t.next;
  364. END;
  365. IF new = NIL THEN (* zero elements in queue *)
  366. (* skip *)
  367. ELSE (* more than one element in queue *)
  368. IF new.next # NIL THEN new.next.prev := new.prev END;
  369. IF new.prev # NIL THEN new.prev.next := new.next END;
  370. IF queue.head = new THEN
  371. queue.head := new.next
  372. END;
  373. IF queue.tail = new THEN
  374. queue.tail := new.prev
  375. END;
  376. new.next := NIL; new.prev := NIL
  377. END;
  378. END GetMaxPrio;
  379. (* Get a process from a queue (NIL if none). Caller must hold lock for specific queue. *)
  380. PROCEDURE Get(VAR queue: ProcessQueue; VAR new: Process);
  381. VAR t: Heaps.ProcessLink;
  382. BEGIN
  383. t := queue.head;
  384. IF t = NIL THEN (* zero elements in queue *)
  385. (* skip *)
  386. ELSIF t = queue.tail THEN (* one element in queue *)
  387. queue.head := NIL; queue.tail := NIL (* {(t.next = NIL) & (t.prev = NIL)} *)
  388. ELSE (* more than one element in queue *)
  389. queue.head := t.next; t.next := NIL; queue.head.prev := NIL
  390. END;
  391. ASSERT((t = NIL) OR (t.next = NIL) & (t.prev = NIL)); (* temp strong check *)
  392. IF t = NIL THEN
  393. new := NIL
  394. ELSE
  395. ASSERT(t IS Process);
  396. new := t(Process)
  397. END;
  398. END Get;
  399. (* Put a process in a queue. Caller must hold lock for specific queue. *)
  400. (* If t was running, be careful to protect Put and the subsequent SwitchTo with the ready lock. *)
  401. PROCEDURE Put(VAR queue: ProcessQueue; t: Process);
  402. BEGIN (* {t # NIL & t.next = NIL} *)
  403. ASSERT((t.next = NIL) & (t.prev = NIL));
  404. IF queue.head = NIL THEN (* queue empty *)
  405. queue.head := t
  406. ELSE (* queue not empty *)
  407. queue.tail.next := t; t.prev := queue.tail
  408. END;
  409. queue.tail := t
  410. END Put;
  411. (* Select a process of at least the specified priority to run next on current processor (returns NIL if none). Caller must hold ready lock. *)
  412. PROCEDURE Select(VAR new: Process; priority: LONGINT);
  413. VAR thresholdPrio: LONGINT;
  414. BEGIN
  415. IF Heaps.gcStatus.GetgcOngoing() THEN
  416. thresholdPrio := GCPriority
  417. ELSE
  418. thresholdPrio := priority
  419. END;
  420. LOOP
  421. IF maxReady < thresholdPrio THEN
  422. IF priority < thresholdPrio THEN Get(ready.q[MinPriority], new) ELSE new := NIL END;
  423. EXIT
  424. END;
  425. Get(ready.q[maxReady], new);
  426. IF (new # NIL) OR (maxReady = MinPriority) THEN EXIT END;
  427. DEC(maxReady)
  428. END
  429. END Select;
  430. (* Enter a process in the ready queue. Caller must hold ready lock. *)
  431. (* If t was running, be careful to make Enter and the subsequent SwitchTo atomic, as the process could be grabbed by another process while it is still running. *)
  432. PROCEDURE Enter(t: Process);
  433. BEGIN
  434. IF Stats THEN Machine.AtomicInc(Nenter) END;
  435. t.mode := Ready;
  436. Put(ready.q[t.priority], t);
  437. IF t.priority > maxReady THEN
  438. maxReady := t.priority (* to do: re-establish global priority invariant *)
  439. END
  440. END Enter;
  441. (* Remove a process from a queue that contains it. Caller must hold lock for specific queue. *)
  442. (* Not intended for frequent use. *)
  443. PROCEDURE Remove(VAR queue: ProcessQueue; t: Process);
  444. BEGIN
  445. IF t.prev # NIL THEN t.prev.next := t.next END;
  446. IF t.next # NIL THEN t.next.prev := t.prev END;
  447. IF t = queue.head THEN queue.head := t.next END;
  448. IF t = queue.tail THEN queue.tail := t.prev END;
  449. ASSERT((queue.head = NIL) OR (queue.head.prev = NIL) & (queue.tail.next = NIL));
  450. t.prev := NIL;
  451. t.next := NIL
  452. END Remove;
  453. (* Switch to the specified process. Caller must hold ready lock. Return may be on different processor! *)
  454. PROCEDURE SwitchTo(VAR running: Process; new: Process); (* parameters used in SwitchToState, TerminateThis, New *)
  455. VAR id: LONGINT;
  456. BEGIN
  457. id := Machine.ID ();
  458. INC (running.cpuCycles[id], Machine.GetTimer () - perfTsc[id]);
  459. (*TRACE(CurrentProcessTime(), perfTsc[id], Machine.GetTimer());*)
  460. IF running.priority = MinPriority THEN (* Special treatment for idle threads *)
  461. INC (idleCycles[id], Machine.GetTimer () - perfTsc[id]);
  462. END;
  463. (* save current state *)
  464. running.state.PC := SYSTEM.PC();(*Machine.CurrentPC ();*) (* for GC *) (* ug *)
  465. running.state.SP := SYSTEM.SP();(*SYSTEM.GetStackPointer ();*) (* for GC *)
  466. running.state.BP := SYSTEM.FP();(*SYSTEM.GetFramePointer ();*) (* save state *)
  467. Machine.FPUSaveMin(running.sse);
  468. running := new; new.mode := Running;
  469. IF Preempted IN new.flags THEN
  470. EXCL(new.flags, Preempted);
  471. perfTsc[id] := Machine.GetTimer ();
  472. (*SYSTEM.SetStackPointer (new.state.SP);*) (* for UpdateState - run on new stack (EBP on old) *)
  473. SYSTEM.SETSP(new.state.SP);
  474. Machine.PushState(new.state);
  475. Machine.FPURestoreFull(new.sse);
  476. Machine.Release(Machine.Objects);
  477. Machine.JumpState (* pops the state parameter from the stack and returns from interrupt *)
  478. ELSE
  479. Machine.FPURestoreMin(new.sse);
  480. perfTsc[id] := Machine.GetTimer ();
  481. SYSTEM.SETSP(new.state.SP);
  482. SYSTEM.SETFP(new.state.BP);
  483. Machine.Release(Machine.Objects);
  484. END
  485. END SwitchTo;
  486. (* Select a new process to run and switch to it. Caller must hold ready lock. *)
  487. PROCEDURE SwitchToNew;
  488. VAR new: Process;
  489. BEGIN
  490. Select(new, MinPriority); (* will return at least an Idle process *)
  491. new.procID := Machine.ID ();
  492. SwitchTo(running[new.procID], new)
  493. END SwitchToNew;
  494. PROCEDURE GetProcessPtr(): Process;
  495. VAR
  496. p: Process;
  497. BEGIN
  498. Machine.Acquire(Machine.Objects);
  499. p := GetProcessPtr0();
  500. Machine.Release(Machine.Objects);
  501. RETURN p
  502. END GetProcessPtr;
  503. PROCEDURE GetProcessPtr0(): Process;
  504. BEGIN
  505. RETURN running[Machine.ID()]
  506. END GetProcessPtr0;
  507. (** Relinquish control. *)
  508. PROCEDURE Yield*;
  509. VAR r, new: Process;
  510. BEGIN
  511. IF ~YieldTrick OR (maxReady >= lowestAllowedPriority) THEN
  512. r := GetProcessPtr ();
  513. Machine.Acquire(Machine.Objects);
  514. Select(new, r.priority);
  515. IF new # NIL THEN (* found another process *)
  516. Enter(r);
  517. new.procID := Machine.ID ();
  518. SwitchTo(running[new.procID], new)
  519. ELSE (* stay with same process *)
  520. Machine.Release(Machine.Objects)
  521. END
  522. END
  523. END Yield;
  524. PROCEDURE SwitchToState(new: Process; VAR state: Machine.State);
  525. BEGIN
  526. (* simulate return from SwitchTo - MOV ESP, EBP; POP EBP; RET 8 *)
  527. state.SP := new.state.BP+AddressSize*2; (* AddressSize*4 is effect of POP, RET AddressSize*2 *)
  528. SYSTEM.GET (new.state.BP, state.BP); (* effect of POP *)
  529. SYSTEM.GET (new.state.BP + AddressSize, state.PC); (* effect of RET *)
  530. END SwitchToState;
  531. (** Preempt the current process. *)
  532. PROCEDURE Timeslice*(VAR state: Machine.State);
  533. VAR id: LONGINT; new: Process;
  534. BEGIN
  535. (* handle a timer tick *)
  536. Machine.Acquire(Machine.Objects);
  537. IF Stats THEN Machine.AtomicInc(Ntimeslice) END;
  538. id := Machine.ID ();
  539. IF id = 0 THEN (* process 0 checks event queues *)
  540. IF event.next.trigger - Machine.ticks <= 0 THEN (* next normal event due *)
  541. IF event.next # event THEN (* not dummy event *)
  542. IF timer # NIL THEN
  543. ASSERT(timer.mode = AwaitingEvent);
  544. Enter(timer); timer := NIL
  545. END
  546. ELSE (* reset dummy event *)
  547. event.trigger := Machine.ticks + MAX(LONGINT) DIV 2 (* ignore overflow *)
  548. END
  549. END
  550. END;
  551. IF Heaps.gcStatus.GetgcOngoing() & (id IN gcBarrier) THEN
  552. EXCL(gcBarrier, id);
  553. IF gcBarrier = {} THEN
  554. ASSERT(gcProcess.mode = AwaitingEvent);
  555. Enter(gcProcess); gcProcess := NIL
  556. END
  557. END;
  558. (* pre-empt the current process *)
  559. IF Machine.PreemptCount(id) = 1 THEN (* check against 1, because we are holding one lock *)
  560. IF running[id].priority # MinPriority THEN (* idle processes are not timesliced *)
  561. Select(new, running[id].priority);
  562. IF new # NIL THEN
  563. INC (running[id].cpuCycles[id], Machine.GetTimer () - perfTsc[id]);
  564. (*TRACE(CurrentProcessTime(), perfTsc[id], Machine.GetTimer());*)
  565. IF Stats THEN Machine.AtomicInc(NtimesliceTaken) END;
  566. INCL(running[id].flags, Preempted);
  567. Machine.CopyState(state, running[id].state);
  568. Machine.FPUSaveFull(running[id].sse); (* to do: floating-point exception possible / Machine.SetupFPU *)
  569. Enter(running[id]);
  570. running[id] := new;
  571. new.mode := Running; new.procID := id;
  572. IF Preempted IN new.flags THEN
  573. EXCL(new.flags, Preempted);
  574. Machine.CopyState(new.state, state);
  575. Machine.FPURestoreFull(new.sse)
  576. ELSE
  577. SwitchToState(new, state);
  578. Machine.FPURestoreMin(new.sse)
  579. END;
  580. perfTsc[id] := Machine.GetTimer ()
  581. ELSE
  582. IF Stats THEN Machine.AtomicInc(NtimesliceNothing) END;
  583. END;
  584. (* Check if the process has the PleasHalt flag and handle it. *)
  585. IF PleaseHalt IN running[id].flags THEN
  586. (* Simulate procedure call: Increase stack & put return PC *)
  587. DEC(state.SP, AddressSize);
  588. SYSTEM.PUT (state.SP, state.PC); (* Here an stack overflow could happen! *)
  589. (* Set the right halt procedure *)
  590. IF (Unbreakable IN running[id].flags) THEN
  591. state.PC := SYSTEM.VAL (ADDRESS, trap[1]);
  592. ELSE
  593. state.PC := SYSTEM.VAL (ADDRESS, trap[0]);
  594. END;
  595. END;
  596. ELSE
  597. INC(idlecount[id]);
  598. IF Stats THEN Machine.AtomicInc(NtimesliceIdle) END;
  599. END
  600. ELSE
  601. IF Stats THEN Machine.AtomicInc(NtimesliceCritical) END (* not preemptable *)
  602. END;
  603. Machine.Release(Machine.Objects)
  604. END Timeslice;
  605. (** Return current process. (DEPRECATED, use ActiveObject) *)
  606. PROCEDURE CurrentProcess*( ): Process;
  607. BEGIN
  608. RETURN GetProcessPtr();
  609. END CurrentProcess;
  610. (** Return current process' context *)
  611. PROCEDURE CurrentContext*(): ANY;
  612. VAR p: Process;
  613. BEGIN
  614. p := CurrentProcess();
  615. IF p # NIL THEN RETURN p.context
  616. ELSE RETURN NIL
  617. END;
  618. END CurrentContext;
  619. PROCEDURE SetContext*(context: ANY);
  620. VAR p: Process;
  621. BEGIN
  622. p := CurrentProcess();
  623. IF p # NIL THEN p.context := context END;
  624. END SetContext;
  625. (* Return stack bottom of process. For compatibility WinAos/UnixAos/NativeAos *)
  626. PROCEDURE GetStackBottom*(p: Process): ADDRESS;
  627. BEGIN
  628. RETURN p.stack.high
  629. END GetStackBottom;
  630. (** Return the active object currently executing. *)
  631. PROCEDURE ActiveObject* (): ANY;
  632. VAR r: Process;
  633. BEGIN
  634. r := GetProcessPtr ();
  635. ASSERT(r # NIL);
  636. ASSERT(r.obj # NIL);
  637. RETURN r.obj
  638. END ActiveObject;
  639. (** Return the ID of the active currently executing process. *)
  640. PROCEDURE GetProcessID* (): LONGINT;
  641. VAR r: Process;
  642. BEGIN
  643. r := GetProcessPtr ();
  644. RETURN r.id
  645. END GetProcessID;
  646. (** Set the current process' priority. *)
  647. PROCEDURE SetPriority*(priority: LONGINT);
  648. VAR id: LONGINT;
  649. BEGIN
  650. ASSERT((priority >= Low) & (priority <= Realtime)); (* priority in bounds *)
  651. IF HandlePriorityInv THEN
  652. Machine.Acquire(Machine.Objects);
  653. id := Machine.ID();
  654. running[id].SetPriority(priority);
  655. Machine.Release(Machine.Objects)
  656. ELSE
  657. id := Machine.AcquirePreemption ();
  658. running[id].priority := priority;
  659. Machine.ReleasePreemption
  660. (* to do: re-establish global priority invariant *)
  661. END
  662. END SetPriority;
  663. (** Return TRUE iff the specified protected object is locked exclusive to the current process. *)
  664. PROCEDURE LockedByCurrent*(obj: ANY): BOOLEAN;
  665. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; id: LONGINT; res: BOOLEAN;
  666. BEGIN
  667. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  668. ASSERT(hdr IS Heaps.ProtRecBlock);
  669. IF HandlePriorityInv THEN
  670. Machine.Acquire(Machine.Objects);
  671. id := Machine.ID();
  672. res := (hdr.lockedBy = running[id]);
  673. Machine.Release(Machine.Objects)
  674. ELSE
  675. id := Machine.AcquirePreemption ();
  676. Machine.AcquireObject(hdr.locked);
  677. res := (hdr.lockedBy = running[id]);
  678. Machine.ReleaseObject(hdr.locked);
  679. Machine.ReleasePreemption;
  680. END;
  681. RETURN res
  682. END LockedByCurrent;
  683. (** Return number of ready and running processes, excluding idle processes. *)
  684. PROCEDURE NumReady* (): LONGINT;
  685. VAR i, n: LONGINT; p: Heaps.ProcessLink;
  686. BEGIN
  687. n := 0;
  688. Machine.Acquire(Machine.Objects);
  689. FOR i := MinPriority+1 TO NumPriorities-1 DO
  690. p := ready.q[i].head; WHILE p # NIL DO INC(n); p := p.next END
  691. END;
  692. FOR i := 0 TO Machine.MaxCPU-1 DO
  693. IF (running[i] # NIL) & (running[i].priority > MinPriority) THEN INC(n) END
  694. END;
  695. Machine.Release(Machine.Objects);
  696. RETURN n
  697. END NumReady;
  698. (** Return number of CPU cycles consumed by the specified process for each processor. If all is TRUE,
  699. return the number of cycles since the process has been created. If FALSE, return the number of cycles
  700. consumed since the last time asked. *)
  701. PROCEDURE GetCpuCycles*(process : Process; VAR cpuCycles : CpuCyclesArray; all : BOOLEAN);
  702. VAR i : LONGINT;
  703. BEGIN
  704. ASSERT(process # NIL);
  705. FOR i := 0 TO Machine.MaxCPU-1 DO cpuCycles[i] := process.cpuCycles[i]; END;
  706. IF ~all THEN
  707. FOR i := 0 TO Machine.MaxCPU-1 DO
  708. cpuCycles[i] := cpuCycles[i] - process.lastCpuCycles[i];
  709. process.lastCpuCycles[i] := process.cpuCycles[i]; (* actually could have changed meanwhile *)
  710. END;
  711. END;
  712. END GetCpuCycles;
  713. (*! DEBUG *)
  714. VAR
  715. currentProcessTime- : HUGEINT;
  716. PROCEDURE CurrentProcessTime * (): HUGEINT;
  717. VAR result: HUGEINT; process: Process; i: LONGINT;
  718. BEGIN
  719. currentProcessTime := Machine.GetTimer();
  720. result := currentProcessTime - perfTsc[Machine.ID()];
  721. process := CurrentProcess();
  722. FOR i := 0 TO Machine.MaxCPU-1 DO result := result + process.cpuCycles[i]; END;
  723. RETURN result;
  724. END CurrentProcessTime;
  725. PROCEDURE TimerFrequency * (): HUGEINT;
  726. BEGIN
  727. RETURN 333000000
  728. END TimerFrequency;
  729. (* Handle hardware interrupt and route it to an interrupt handler process. *)
  730. PROCEDURE FieldIRQ(VAR state: Machine.State);
  731. VAR t: Process; id: LONGINT; new: Process; preempt: BOOLEAN;
  732. BEGIN
  733. Machine.DisableIRQ(state.INT); (* do this before acknowledging irq *)
  734. IF StrongChecks THEN
  735. IF processingIRQ[state.INT-MinIRQ] THEN
  736. (*Trace.String("IRQ recursion "); Trace.Address(state.INT); Trace.Ln;*)
  737. RETURN
  738. ELSE
  739. processingIRQ[state.INT-MinIRQ] := TRUE;
  740. END;
  741. END;
  742. (* if the reenabling of interrupts cannot be circumvented, then it is REQUIRED to acknowledge interrupts
  743. BEFORE reenabling. Otherwise spurious IRQs cannot be identified as such.
  744. Please note that this particular problem with spurious IRQs cannot observed on many machines but IF it is observed
  745. then the machine will behave unexpected. Very hard to debug and trace!
  746. Machine.Ack(state.INT);
  747. Machine.Sti (); (* avoid Processors.StopAll deadlock when waiting for locks below (remove this) *)
  748. *)
  749. Machine.Acquire(Machine.Objects);
  750. (*IF state.INT = 53 THEN Trace.String("|") END;*)
  751. t := interrupt[state.INT-MinIRQ].process;
  752. IF StrongChecks THEN ASSERT(t.mode = AwaitingEvent) END;
  753. id := Machine.ID ();
  754. preempt := (t.priority > maxReady) & (maxReady # MinPriority) & (t.priority > running[id].priority);
  755. Enter(t);
  756. IF preempt THEN
  757. IF Stats THEN Machine.AtomicInc(Npreempt) END;
  758. (* pre-empt the current process *)
  759. IF Machine.PreemptCount(id) = 1 THEN (* check against 1, because we are holding one lock *)
  760. Select(new, running[id].priority + 1);
  761. IF new # NIL THEN
  762. INC (running[id].cpuCycles[id], Machine.GetTimer () - perfTsc[id]);
  763. IF running[id].priority = MinPriority THEN (* Special treatment for idle threads *)
  764. INC (idleCycles[id], Machine.GetTimer () - perfTsc[id]);
  765. END;
  766. IF Stats THEN Machine.AtomicInc(NpreemptTaken) END;
  767. INCL(running[id].flags, Preempted);
  768. Machine.CopyState(state, running[id].state);
  769. Machine.FPUSaveFull(running[id].sse); (* to do: floating-point exception possible / Machine.SetupFPU *)
  770. Enter(running[id]);
  771. running[id] := new;
  772. new.mode := Running; new.procID := id;
  773. IF Preempted IN new.flags THEN
  774. EXCL(new.flags, Preempted);
  775. Machine.CopyState(new.state, state);
  776. Machine.FPURestoreFull(new.sse)
  777. ELSE
  778. SwitchToState(new, state);
  779. Machine.FPURestoreMin(new.sse)
  780. END;
  781. perfTsc[id] := Machine.GetTimer ()
  782. ELSE
  783. IF Stats THEN Machine.AtomicInc(NpreemptNothing) END
  784. END
  785. ELSE
  786. IF Stats THEN Machine.AtomicInc(NpreemptCritical) END (* not preemptable *)
  787. END
  788. END;
  789. Machine.Release(Machine.Objects)
  790. END FieldIRQ;
  791. (* Process scheduled to handle an interrupt. *)
  792. PROCEDURE InterruptProcess(self: ProtectedObject);
  793. VAR h: InterruptList; t: Process; int: LONGINT;
  794. BEGIN
  795. int := self(Interrupter).interruptNumber;
  796. t := interrupt[int-MinIRQ].process;
  797. LOOP
  798. h := interrupt[int-MinIRQ].root; (* concurrent updates allowed in InstallHandler and RemoveHandler *)
  799. WHILE h # NIL DO h.handler (); h := h.next END;
  800. Machine.Acquire(Machine.Objects);
  801. ASSERT(running[Machine.ID ()] = t); (* strong check *)
  802. t.mode := AwaitingEvent;
  803. processingIRQ[int-MinIRQ] := FALSE;
  804. Machine.EnableIRQ(int);
  805. SwitchToNew
  806. END
  807. END InterruptProcess;
  808. (** Install interrupt handler. *)
  809. PROCEDURE InstallHandler*(h: EventHandler; int: LONGINT);
  810. VAR t: Process; new: BOOLEAN; ih: Interrupter; n: InterruptList; i: LONGINT;
  811. BEGIN
  812. ASSERT((int >= MinIRQ) & (int-MinIRQ < NumIRQ)); (* range check *)
  813. IF interrupt[int-MinIRQ].process = NIL THEN (* first handler for this irq *)
  814. (* allocate process outside lock region, to avoid GC lock problems. *)
  815. (* hack: use type parameter to pass int index & set obj to h, for System.ShowProcesses *)
  816. NEW(ih); ih.interruptNumber := int;
  817. NewProcess(InterruptProcess, {Resistant}, ih, t);
  818. t.priority := High; (* second-level interrupt handling processes have high priority, handlers may allocate memory, use exclusive locks and awaits *)
  819. t.staticPriority := t.priority;
  820. FOR i := 0 TO LEN(t.prioRequests) - 1 DO t.prioRequests[i] := 0 END;
  821. INC(t.prioRequests[t.priority])
  822. END;
  823. NEW(n); n.handler := h;
  824. Machine.Acquire(Machine.Objects);
  825. IF interrupt[int-MinIRQ].process = NIL THEN (* still first handler for this irq *)
  826. t.id := nextProcessID; INC(nextProcessID);
  827. t.mode := AwaitingEvent;
  828. interrupt[int-MinIRQ].process := t;
  829. new := TRUE
  830. ELSE
  831. new := FALSE
  832. END;
  833. n.next := interrupt[int-MinIRQ].root; (* can be concurrent with loop in InterruptProcess *)
  834. interrupt[int-MinIRQ].root := n;
  835. Machine.Release(Machine.Objects);
  836. IF new THEN Machine.InstallHandler(FieldIRQ, int) END (* do outside lock region to avoid NEW/GC deadlock *)
  837. END InstallHandler;
  838. (** Remove interrupt handler. *)
  839. PROCEDURE RemoveHandler*(h: EventHandler; int: LONGINT);
  840. VAR p, c: InterruptList;
  841. BEGIN
  842. ASSERT((int >= MinIRQ) & (int-MinIRQ < NumIRQ)); (* range check *)
  843. Machine.Acquire(Machine.Objects);
  844. p := NIL; c := interrupt[int-MinIRQ].root;
  845. WHILE (c.handler # h) & (c # NIL) DO p := c; c := c.next END;
  846. IF c.handler = h THEN (* handler found *)
  847. IF p = NIL THEN
  848. interrupt[int-MinIRQ].root := c.next;
  849. (*
  850. IF c.inext = NIL THEN (* this was the last handler *)
  851. Machine.RemoveHandler(FieldIRQ, int)
  852. (* to do: synchronize with FieldIRQ and InterruptProcess *)
  853. END
  854. *)
  855. ELSE
  856. p.next := c.next
  857. END
  858. ELSE
  859. HALT(99); (* handler not found *)
  860. END;
  861. (* can not clear c.next field, because InterruptProcess may be traversing it. *)
  862. Machine.Release(Machine.Objects)
  863. END RemoveHandler;
  864. (* local procedure *)
  865. PROCEDURE SetTimeoutAbsOrRel(t: Timer; h: EventHandler; ms: LONGINT; isAbsolute: BOOLEAN);
  866. VAR e: Timer; trigger: LONGINT;
  867. BEGIN
  868. ASSERT(Machine.Second= 1000); (* assume milliseconds for now *)
  869. ASSERT((t # NIL) & (h # NIL));
  870. IF ms < 1 THEN ms := 1 END;
  871. Machine.Acquire(Machine.Objects);
  872. IF isAbsolute THEN trigger := ms ELSE trigger := Machine.ticks + ms (* ignore overflow *) END;
  873. IF t.next # NIL THEN (* cancel previous timeout *)
  874. t.next.prev := t.prev; t.prev.next := t.next
  875. END;
  876. t.trigger := trigger; t.handler := h;
  877. e := event.next; (* performance: linear search! *)
  878. WHILE (e # event) & (e.trigger - trigger <= 0) DO e := e.next END;
  879. t.prev := e.prev; e.prev := t; t.next := e; t.prev.next := t;
  880. Machine.Release(Machine.Objects)
  881. END SetTimeoutAbsOrRel;
  882. (** Set (or reset) an event handler object's timeout value. *)
  883. PROCEDURE SetTimeout*(t: Timer; h: EventHandler; ms: LONGINT);
  884. BEGIN
  885. SetTimeoutAbsOrRel(t, h, ms, FALSE)
  886. END SetTimeout;
  887. (** Set (or reset) an event handler object's timeout value. Here ms is absolute *)
  888. PROCEDURE SetTimeoutAt*(t: Timer; h: EventHandler; ms: LONGINT);
  889. BEGIN
  890. SetTimeoutAbsOrRel(t, h, ms, TRUE)
  891. END SetTimeoutAt;
  892. (** Cancel an event handler object's timeout, if any. It is possible that the timer has expired, but not yet been scheduled to run. *)
  893. PROCEDURE CancelTimeout*(t: Timer);
  894. BEGIN
  895. Machine.Acquire(Machine.Objects);
  896. ASSERT(t # event);
  897. IF t.next # NIL THEN
  898. t.next.prev := t.prev; t.prev.next := t.next;
  899. t.next := NIL; t.prev := NIL
  900. END;
  901. Machine.Release(Machine.Objects)
  902. END CancelTimeout;
  903. (** Terminate the current process and switch to next process. *)
  904. PROCEDURE Terminate*; (* exported for Linker *)
  905. VAR id: LONGINT;
  906. BEGIN
  907. IF Stats THEN Machine.AtomicInc(Nterminate) END;
  908. Machine.Acquire(Machine.Objects);
  909. id := Machine.ID ();
  910. (*running[id].state.PC := CallerPC ();*) (* for tracing *)
  911. running[id].mode := Terminated; (* a process can also be "terminated" if the queue containing it is garbage collected *)
  912. SwitchToNew;
  913. HALT(2201) (* process resurrected *)
  914. END Terminate;
  915. PROCEDURE Halt;
  916. BEGIN
  917. HALT(halt); (* process halted *)
  918. END Halt;
  919. PROCEDURE HaltUnbreakable;
  920. BEGIN
  921. HALT(haltUnbreakable); (* process halted *)
  922. END HaltUnbreakable;
  923. (* Set the return PC which is saved in the process and set it to -1 *)
  924. PROCEDURE HaltAltPC(haltCode: LONGINT);
  925. VAR bp: ADDRESS; p: Process;
  926. BEGIN
  927. p := running[Machine.ID ()];
  928. ASSERT(p.oldReturnPC # -1);
  929. bp := SYSTEM.GetFramePointer ();
  930. SYSTEM.PUT (bp + AddressSize, p.oldReturnPC);
  931. CASE haltCode OF
  932. |halt: HALT(halt);
  933. |haltUnbreakable: HALT(haltUnbreakable);
  934. END;
  935. END HaltAltPC;
  936. PROCEDURE HaltReturn;
  937. VAR bp: ADDRESS;
  938. BEGIN
  939. bp := SYSTEM.GetFramePointer ();
  940. SYSTEM.GET (bp, bp); (* Get the dynamic link *)
  941. SYSTEM.SetFramePointer (bp); (* Undo the actual paf *)
  942. HaltAltPC(halt);
  943. END HaltReturn;
  944. PROCEDURE HaltUnbreakableReturn;
  945. VAR bp: ADDRESS;
  946. BEGIN
  947. bp := SYSTEM.GetFramePointer ();
  948. SYSTEM.GET (bp, bp); (* Get the dynamic link *)
  949. SYSTEM.SetFramePointer (bp); (* Undo the actual paf *)
  950. HaltAltPC(haltUnbreakable);
  951. END HaltUnbreakableReturn;
  952. PROCEDURE TerminateThis*(t: Process; unbreakable: BOOLEAN);
  953. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; pc, fp : ADDRESS;
  954. (* terminates a process that is either in mode AwaitingLock or AwaitingCond *)
  955. PROCEDURE TerminateAwaiting(t: Process);
  956. VAR hdr {UNTRACED}: Heaps.ProtRecBlock;
  957. BEGIN
  958. SYSTEM.GET(SYSTEM.VAL(ADDRESS, t.waitingOn) + Heaps.HeapBlockOffset, hdr);
  959. ASSERT(hdr IS Heaps.ProtRecBlock);
  960. IF t.mode = AwaitingLock THEN
  961. fp := t.state.BP; (* SwitchTo PAF *)
  962. SYSTEM.GET (fp, fp); (* SwitchToNew PAF *)
  963. SYSTEM.GET (fp, fp); (* Lock PAF*)
  964. SYSTEM.GET (fp + AddressSize, pc); (* Get the return address*)
  965. IF ~Modules.IsExceptionHandled(pc, fp, FALSE) THEN
  966. Remove(hdr.awaitingLock, t);
  967. t.waitingOn := NIL; SYSTEM.GET (t.state.BP + AddressSize, t.oldReturnPC);
  968. IF unbreakable THEN
  969. SYSTEM.PUT (t.state.BP + AddressSize, SYSTEM.VAL (ADDRESS, trapReturn[1]))
  970. ELSE
  971. SYSTEM.PUT (t.state.BP + AddressSize, SYSTEM.VAL (ADDRESS, trapReturn[0]))
  972. END;
  973. Enter(t)
  974. ELSE
  975. Machine.Acquire (Machine.TraceOutput);
  976. Trace.String(" Not allowed to kill "); Trace.Int(t.id, 1); Trace.Char(" "); Trace.Int(t.mode, 1); Trace.Ln;
  977. Machine.Release (Machine.TraceOutput);
  978. END
  979. ELSIF t.mode = AwaitingCond THEN
  980. SYSTEM.GET (t.state.BP, fp);
  981. SYSTEM.GET (t.state.PC, pc);
  982. IF ~Modules.IsExceptionHandled(pc, fp, TRUE) THEN
  983. Remove(hdr.awaitingCond, t);
  984. t.waitingOn := NIL; SYSTEM.GET (t.state.BP + AddressSize, t.oldReturnPC);
  985. IF unbreakable THEN
  986. SYSTEM.PUT (t.state.BP + AddressSize, SYSTEM.VAL (ADDRESS, trapReturn[1]))
  987. ELSE
  988. SYSTEM.PUT (t.state.BP + AddressSize, SYSTEM.VAL (ADDRESS, trapReturn[0]))
  989. END;
  990. Enter(t)
  991. ELSE
  992. Machine.Acquire (Machine.TraceOutput);
  993. Trace.String(" Not allowed to kill "); Trace.Int(t.id, 1); Trace.Char(" "); Trace.Int(t.mode, 1); Trace.Ln;
  994. Machine.Release (Machine.TraceOutput);
  995. END
  996. END
  997. END TerminateAwaiting;
  998. BEGIN
  999. IF PleaseHalt IN t.flags THEN
  1000. IF TraceVerbose THEN
  1001. Machine.Acquire (Machine.TraceOutput);
  1002. Trace.String("Process (ID="); Trace.Int(t.id, 0); Trace.StringLn (") is already halting!");
  1003. Machine.Release (Machine.TraceOutput);
  1004. END;
  1005. RETURN
  1006. ELSE
  1007. Machine.Acquire(Machine.Objects);
  1008. IF (t = running[Machine.ID ()]) THEN INCL(t.flags, SelfTermination); END;
  1009. IF TraceVerbose THEN
  1010. Machine.Acquire (Machine.TraceOutput);
  1011. Trace.String(" Kill "); Trace.Int(t.id, 1); Trace.Char(" "); Trace.Int(t.mode, 1); Trace.Ln;
  1012. Machine.Release (Machine.TraceOutput);
  1013. END;
  1014. CASE t.mode OF
  1015. |Running:
  1016. INCL(t.flags, PleaseHalt);
  1017. IF unbreakable THEN INCL(t.flags, Unbreakable) END
  1018. |Ready:
  1019. DEC(t.state.SP, AddressSize); SYSTEM.PUT (t.state.SP, t.state.PC);
  1020. IF unbreakable THEN t.state.PC := SYSTEM.VAL (ADDRESS, trap[1])
  1021. ELSE t.state.PC := SYSTEM.VAL (ADDRESS, trap[0]) END
  1022. |AwaitingLock, AwaitingCond:
  1023. IF HandlePriorityInv THEN
  1024. TerminateAwaiting(t)
  1025. ELSE
  1026. SYSTEM.GET(SYSTEM.VAL(ADDRESS, t.waitingOn) + Heaps.HeapBlockOffset, hdr);
  1027. ASSERT(hdr IS Heaps.ProtRecBlock);
  1028. IF ~hdr.locked THEN
  1029. Machine.AcquireObject(hdr.locked);
  1030. TerminateAwaiting(t);
  1031. Machine.ReleaseObject(hdr.locked)
  1032. END
  1033. END
  1034. | AwaitingEvent, Unknown, Terminated: (* skip *)
  1035. END;
  1036. Machine.Release(Machine.Objects)
  1037. END
  1038. END TerminateThis;
  1039. (* called by WMProcessInfo to obtain the current state of a running process *)
  1040. PROCEDURE UpdateProcessState*( p: Process );
  1041. BEGIN
  1042. (* update p.stat.{PC,BP,SP} *)
  1043. END UpdateProcessState;
  1044. (* Finalize a process. *)
  1045. PROCEDURE FinalizeProcess(t: ANY);
  1046. BEGIN
  1047. Machine.DisposeStack(t(Process).stack)
  1048. END FinalizeProcess;
  1049. (* Allocate a new process associated with "obj". Must be outside lock region, because of potential GC. *)
  1050. PROCEDURE NewProcess(body: Body; flags: SET; obj: ProtectedObject; VAR new: Process);
  1051. VAR t: Process; sp: ADDRESS; id: LONGINT; fn: Heaps.FinalizerNode;
  1052. BEGIN
  1053. NEW(t); NEW(fn); (* implicit call Heaps.NewRec *)
  1054. t.next := NIL; t.prev := NIL; t.rootedNext := NIL;
  1055. t.waitingOn := NIL; t.flags := flags;
  1056. t.obj := obj; t.mode := Unknown;
  1057. (* initialize the stack *)
  1058. Machine.NewStack(t.stack, t, sp);
  1059. IF VeryConservative THEN
  1060. Machine.Fill32(t.stack.adr, sp-t.stack.adr, LONGINT(0D0D0DEADH))
  1061. END;
  1062. SYSTEM.PUT (sp-1*AddressSize, obj); (* self parameter for body *)
  1063. SYSTEM.PUT (sp-2*AddressSize, terminate); (* return address for body *)
  1064. SYSTEM.PUT (sp-3*AddressSize, NIL); (* FP for body *)
  1065. (* the following two are not necessary because the compiler instruments the caller to cleanup parameters, not the callee! *)
  1066. (*SYSTEM.PUT (sp-3*AddressSize, NIL);*) (* parameter for SwitchTo (ADR(running)) *)
  1067. (*SYSTEM.PUT (sp-4*AddressSize, NIL);*) (* parameter for SwitchTo (new) *)
  1068. SYSTEM.PUT (sp-4*AddressSize, SYSTEM.VAL(ADDRESS, body) + ReturnStackDisplacement); (* return address for SwitchTo (body entry point) *)
  1069. SYSTEM.PUT (sp-5*AddressSize, sp-3*AddressSize); (* end of dynamic link list (FP value at entry to body) *)
  1070. t.sseAdr := ADDRESSOF(t.sse) + ((-ADDRESSOF(t.sse)) MOD 16);
  1071. Machine.FPUSaveMin(t.sse); (* inherit FPU state of caller *)
  1072. t.state.BP := sp - 5*AddressSize;
  1073. t.state.SP := t.state.BP;
  1074. t.state.PC := 0; (* indicating that process is not running yet *)
  1075. (* set up exception handling *)
  1076. IF Restart IN flags THEN (* restart object body *)
  1077. t.restartPC := SYSTEM.VAL (ADDRESS, body) + ReturnStackDisplacement;
  1078. t.restartSP := sp-3*AddressSize (* 1 parameter and return address of body *)
  1079. ELSE (* terminate process *)
  1080. t.restartPC := SYSTEM.VAL (ADDRESS, terminate) + ReturnStackDisplacement;
  1081. t.restartSP := sp-AddressSize
  1082. END;
  1083. fn.finalizer := FinalizeProcess;
  1084. Heaps.AddFinalizer(t, fn);
  1085. (* return *)
  1086. FOR id := 0 TO Machine.MaxCPU-1 DO t.cpuCycles[id] := 0 END;
  1087. new := t
  1088. END NewProcess;
  1089. (* Create the process associated with an active object (kernel call). *)
  1090. PROCEDURE CreateProcess*(body: Body; priority: LONGINT; flags: SET; obj: ProtectedObject);
  1091. VAR t: Process; type: ADDRESS; heapBlock {UNTRACED}: Heaps.HeapBlock; i: LONGINT;
  1092. BEGIN
  1093. IF Stats THEN Machine.AtomicInc(Ncreate) END;
  1094. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, heapBlock);
  1095. ASSERT(heapBlock IS Heaps.ProtRecBlock); (* protected object *)
  1096. SYSTEM.GET (SYSTEM.VAL (ADDRESS, obj) + Heaps.TypeDescOffset, type); (* type tag *)
  1097. IF Restart IN flags THEN INCL(flags, Resistant) END; (* SAFE => Restart & Resistant *)
  1098. NewProcess(body, flags, obj, t);
  1099. Machine.Acquire(Machine.Objects);
  1100. t.id := nextProcessID; INC(nextProcessID);
  1101. IF priority = 0 THEN (* no priority specified *)
  1102. t.priority := running[Machine.ID ()].priority (* inherit priority of creator *)
  1103. ELSIF priority > 0 THEN (* positive priority specified *)
  1104. t.priority := priority
  1105. ELSE (* negative priority specified (only for Idle process) *)
  1106. t.priority := MinPriority
  1107. END;
  1108. t.staticPriority := t.priority;
  1109. FOR i := 0 TO LEN(t.prioRequests) - 1 DO t.prioRequests[i] := 0 END;
  1110. INC(t.prioRequests[t.priority]);
  1111. CASE t.priority OF
  1112. MinPriority : t.rootedNext := rootedProcesses[t.priority]; rootedProcesses[t.priority] := t
  1113. | Low, Normal, High : (* do nothing, processes with this priority are traced by GC automatically *)
  1114. | GCPriority, Realtime : t.rootedNext := rootedProcesses[t.priority]; rootedProcesses[t.priority] := t
  1115. END;
  1116. Enter(t);
  1117. Machine.Release(Machine.Objects)
  1118. END CreateProcess;
  1119. (* Lock a protected object (kernel call) *)
  1120. (* There are two different procedures for locking a protected object in case of priority inversion handling enabled or disabled due to the different
  1121. locking strategy. *)
  1122. PROCEDURE Lock*(obj: ProtectedObject; exclusive: BOOLEAN);
  1123. BEGIN
  1124. IF HandlePriorityInv THEN
  1125. LockPriorityInv(obj, exclusive)
  1126. ELSE
  1127. LockNoPriorityInv(obj, exclusive)
  1128. END
  1129. END Lock;
  1130. (* Lock a protected object if priority inversion handling is disabled. Header locks, preemption and Machine.Objects locks are used. *)
  1131. PROCEDURE LockNoPriorityInv(obj: ProtectedObject; exclusive: BOOLEAN);
  1132. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; r: Process; id: LONGINT;
  1133. BEGIN (* {called from user level} *)
  1134. IF Stats THEN Machine.AtomicInc(Nlock) END;
  1135. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  1136. IF StrongChecks THEN
  1137. ASSERT(hdr IS Heaps.ProtRecBlock); (* protected object *)
  1138. ASSERT(exclusive) (* shared not implemented yet *)
  1139. END;
  1140. id := Machine.AcquirePreemption ();
  1141. Machine.AcquireObject(hdr.locked);
  1142. IF hdr.count = 0 THEN (* not locked *)
  1143. hdr.count := -1; hdr.lockedBy := GetProcessPtr (); (* set exclusive lock *)
  1144. Machine.ReleaseObject(hdr.locked);
  1145. Machine.ReleasePreemption;
  1146. ELSE (* locked *)
  1147. r := GetProcessPtr ();
  1148. IF hdr.lockedBy = r THEN
  1149. Machine.ReleaseObject(hdr.locked);
  1150. Machine.ReleasePreemption;
  1151. ASSERT(hdr.lockedBy # r, 2203); (* nested locks not allowed *)
  1152. END;
  1153. ASSERT(r.waitingOn = NIL);
  1154. r.waitingOn := obj; r.mode := AwaitingLock;
  1155. Machine.Acquire(Machine.Objects);
  1156. Put(hdr.awaitingLock, r);
  1157. Machine.ReleaseObject(hdr.locked);
  1158. Machine.ReleasePreemption;
  1159. SwitchToNew
  1160. END
  1161. END LockNoPriorityInv;
  1162. (*
  1163. (* propagation of priorities - lock Machine.Objects is taken.
  1164. This is a procedure that calls itself recursively if a higher priority is propagated along a chain of resources and processes where each resource
  1165. is locked by a process that itself waits on a resource. The procedure can be rewritten into a non-recursive procedure if needed..
  1166. Remark: parameters of type Heaps.HeapBlock or extensions of it are not passed as parameters for clarity and safety reasons .
  1167. Instead, a ProtectedObject pointer is passed as the first parameter. *)
  1168. PROCEDURE PropagatePrio(obj: ProtectedObject; prevMaxWaitingPrio, waitingPrio: LONGINT);
  1169. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; p: Process;
  1170. BEGIN
  1171. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  1172. IF hdr.lockedBy # NIL THEN
  1173. p := hdr.lockedBy(Process);
  1174. DEC(p.prioRequests[prevMaxWaitingPrio]);
  1175. INC(p.prioRequests[waitingPrio]);
  1176. IF (p.waitingOn # NIL) & (waitingPrio > p.priority) THEN
  1177. obj := p.waitingOn;
  1178. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  1179. prevMaxWaitingPrio := MaxPrio(hdr.waitingPriorities);
  1180. DEC(hdr.waitingPriorities[p.priority]);
  1181. INC(hdr.waitingPriorities[waitingPrio]);
  1182. IF waitingPrio > prevMaxWaitingPrio THEN PropagatePrio(obj, prevMaxWaitingPrio, waitingPrio) END
  1183. END;
  1184. IF waitingPrio > p.priority THEN
  1185. IF p.mode = Ready THEN Remove(ready.q[p.priority], p) END; (* remove p from the lower priority queue ... *)
  1186. p.priority := waitingPrio;
  1187. IF p.mode = Ready THEN Enter(p) END; (* ... and add it to the higher priority queue *)
  1188. END
  1189. END;
  1190. END PropagatePrio;
  1191. *)
  1192. (* propagation of priorities - lock Machine.Objects is taken.
  1193. This procedure is the iterative version of the above commented out recursive procedure.
  1194. Remark: hdr is an actually UNTRACED parameter. The GC, however, can handle this, see procedure Heaps.Mark, there is a check whether the
  1195. pointer to the header part is valid. In case of hdr, the pointer ot the header part is NIL. *)
  1196. PROCEDURE PropagatePrio(hdr: Heaps.ProtRecBlock; prevMaxWaitingPrio, waitingPrio: LONGINT);
  1197. VAR propagateFurther: BOOLEAN; p: Process; obj: ProtectedObject;
  1198. BEGIN
  1199. propagateFurther := TRUE;
  1200. WHILE propagateFurther & (waitingPrio > prevMaxWaitingPrio) DO
  1201. IF hdr.lockedBy # NIL THEN
  1202. p := hdr.lockedBy(Process);
  1203. DEC(p.prioRequests[prevMaxWaitingPrio]);
  1204. INC(p.prioRequests[waitingPrio]);
  1205. IF (p.waitingOn # NIL) & (waitingPrio > p.priority) THEN
  1206. obj := p.waitingOn;
  1207. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  1208. prevMaxWaitingPrio := MaxPrio(hdr.waitingPriorities);
  1209. DEC(hdr.waitingPriorities[p.priority]);
  1210. INC(hdr.waitingPriorities[waitingPrio]);
  1211. ELSE (* p is not waiting for a resource or waitingPrio is less or equal to p's priority - priority propagation finishes *)
  1212. propagateFurther := FALSE
  1213. END;
  1214. IF waitingPrio > p.priority THEN (* independently of whether p is waiting on a resource or not the priority of p is changed if it is lower than waitingPrio *)
  1215. IF p.mode = Ready THEN Remove(ready.q[p.priority], p) END; (* remove p from the lower priority queue ... *)
  1216. p.priority := waitingPrio;
  1217. IF p.mode = Ready THEN Enter(p) END; (* ... and add it to the higher priority queue *)
  1218. END
  1219. ELSE (* current resource is not locked - priority propagation finishes *)
  1220. propagateFurther := FALSE
  1221. END
  1222. END
  1223. END PropagatePrio;
  1224. (* TO DO: adapt priority inversion algorithm such that priority of a process is not raised higher than High, it must not become Realtime, otherwise
  1225. GC may be corrupted *)
  1226. (* Lock a protected object if priority inversion handling is enabled. Machine.Objects lock is used. *)
  1227. PROCEDURE LockPriorityInv(obj: ProtectedObject; exclusive: BOOLEAN);
  1228. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; r: Process;
  1229. maxWaitingPrio, prevMaxWaitingPrio: LONGINT;
  1230. BEGIN (* {called from user level} *)
  1231. IF Stats THEN Machine.AtomicInc(Nlock) END;
  1232. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  1233. IF StrongChecks THEN
  1234. ASSERT(hdr IS Heaps.ProtRecBlock); (* protected object *)
  1235. ASSERT(exclusive) (* shared not implemented yet *)
  1236. END;
  1237. Machine.Acquire(Machine.Objects);
  1238. r := (*GetProcessPtr0();*) running[Machine.ID()];
  1239. ASSERT(r # NIL);
  1240. IF hdr.count = 0 THEN (* not locked *)
  1241. hdr.count := -1; hdr.lockedBy := r; (* set exclusive lock *)
  1242. maxWaitingPrio := MaxPrio(hdr.waitingPriorities);
  1243. INC(r.prioRequests[maxWaitingPrio]);
  1244. r.priority := MaxPrio(r.prioRequests);
  1245. Machine.Release(Machine.Objects);
  1246. ELSE (* locked (to do: on multiprocessors, perhaps spin here for a while, if lockedBy.mode = running) *)
  1247. IF hdr.lockedBy = NIL THEN
  1248. Machine.Release(Machine.Objects);
  1249. ASSERT(hdr.lockedBy # NIL)
  1250. END;
  1251. IF hdr.lockedBy = r THEN
  1252. Machine.Release(Machine.Objects);
  1253. ASSERT(hdr.lockedBy # r, 2203); (* nested locks not allowed *)
  1254. END;
  1255. IF r.waitingOn # NIL THEN
  1256. Machine.Acquire(Machine.TraceOutput);
  1257. Trace.String("Objects: LockPriorityInv - hdr.count # NIL, but r.waitingOn # NIL");
  1258. Machine.Release(Machine.TraceOutput)
  1259. END;
  1260. ASSERT(r.waitingOn = NIL);
  1261. r.waitingOn := obj; r.mode := AwaitingLock;
  1262. prevMaxWaitingPrio := MaxPrio(hdr.waitingPriorities);
  1263. INC(hdr.waitingPriorities[r.priority]);
  1264. IF r.priority > prevMaxWaitingPrio THEN PropagatePrio(hdr, prevMaxWaitingPrio, r.priority) END;
  1265. Put(hdr.awaitingLock, r);
  1266. SwitchToNew
  1267. END
  1268. END LockPriorityInv;
  1269. (* Find the first true condition from the queue and remove it. Assume the object is currently locked. *)
  1270. PROCEDURE FindCondition(VAR q: ProcessQueue): Process;
  1271. VAR first, cand: Process;
  1272. BEGIN
  1273. IF Stats THEN Machine.AtomicInc(Ncondition) END;
  1274. Get(q, first);
  1275. IF first.condition(first.condFP) THEN
  1276. IF Stats THEN Machine.AtomicInc(Ncondition1True) END;
  1277. RETURN first
  1278. END;
  1279. Put(q, first);
  1280. WHILE q.head # first DO
  1281. IF Stats THEN Machine.AtomicInc(Ncondition2) END;
  1282. Get(q, cand);
  1283. IF cand.condition(cand.condFP) THEN
  1284. IF Stats THEN Machine.AtomicInc(Ncondition2True) END;
  1285. RETURN cand
  1286. END;
  1287. Put(q, cand)
  1288. END;
  1289. RETURN NIL
  1290. END FindCondition;
  1291. (* Find highest priority in array of priority counts *)
  1292. PROCEDURE MaxPrio(CONST priorityCounts: ARRAY OF LONGINT): LONGINT;
  1293. VAR i: LONGINT;
  1294. BEGIN
  1295. i := LEN(priorityCounts) - 1;
  1296. WHILE (i >= 0) & (priorityCounts[i] = 0) DO DEC(i) END;
  1297. IF priorityCounts[i] = 0 THEN
  1298. Machine.Acquire(Machine.TraceOutput);
  1299. Trace.StringLn("Objects: MaxPrio - SEVERE ERROR: priorityCounts contains all zeros");
  1300. Machine.Release(Machine.TraceOutput);
  1301. END;
  1302. RETURN i
  1303. END MaxPrio;
  1304. (* Unlock a protected object (kernel call). *)
  1305. (* There are two different procedures for locking a protected object in case of priority inverison handling enabled or disabled due to the different
  1306. locking strategy. *)
  1307. PROCEDURE Unlock*(obj: ProtectedObject; dummy: BOOLEAN);
  1308. BEGIN
  1309. IF HandlePriorityInv THEN
  1310. UnlockPriorityInv(obj)
  1311. ELSE
  1312. UnlockNoPriorityInv(obj)
  1313. END
  1314. END Unlock;
  1315. (* transfer the lock from a resource to another process.
  1316. Remark: hdr is an actually UNTRACED parameter. The GC, however, can handle this, see procedure Heaps.Mark, there is a check whether the
  1317. pointer to the header part is valid. In case of hdr, the pointer ot the header part is NIL. *)
  1318. PROCEDURE TransferLock(hdr: Heaps.ProtRecBlock; p: Process);
  1319. VAR maxWaitingPrio: LONGINT;
  1320. BEGIN
  1321. ASSERT(p # NIL);
  1322. p.waitingOn := NIL; hdr.lockedBy := p;
  1323. IF HandlePriorityInv THEN
  1324. DEC(hdr.waitingPriorities[p.priority]);
  1325. maxWaitingPrio := MaxPrio(hdr.waitingPriorities);
  1326. INC(p.prioRequests[maxWaitingPrio]);
  1327. p.priority := MaxPrio(p.prioRequests)
  1328. END
  1329. END TransferLock;
  1330. (* Unlock a protected object if priority inversion handling is disabled. Header locks, preemption and Machine.Objects locks are used. *)
  1331. PROCEDURE UnlockNoPriorityInv(obj: ProtectedObject);
  1332. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; t, c, r: Process; id: LONGINT;
  1333. BEGIN
  1334. IF Stats THEN Machine.AtomicInc(Nunlock) END;
  1335. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  1336. IF StrongChecks THEN
  1337. ASSERT(hdr IS Heaps.ProtRecBlock) (* protected object *)
  1338. END;
  1339. ASSERT(hdr.count = -1); (* exclusive locked *)
  1340. IF hdr.awaitingCond.head # NIL THEN (* evaluate the waiting conditions *)
  1341. (* we are holding the lock, so the queue can not change (to do: except in TerminateThis) *)
  1342. c := FindCondition(hdr.awaitingCond) (* interrupts should be on during this call *)
  1343. ELSE
  1344. c := NIL
  1345. END;
  1346. id := Machine.AcquirePreemption ();
  1347. Machine.AcquireObject(hdr.locked);
  1348. r := running[Machine.ID ()];
  1349. ASSERT(r # NIL);
  1350. IF hdr.lockedBy # r THEN
  1351. Machine.ReleaseObject(hdr.locked);
  1352. Machine.ReleasePreemption;
  1353. ASSERT(hdr.lockedBy = r)
  1354. END;
  1355. IF c = NIL THEN (* no true condition found, check the lock queue *)
  1356. Get(hdr.awaitingLock, t);
  1357. IF t # NIL THEN
  1358. IF StrongChecks THEN
  1359. ASSERT((t.mode = AwaitingLock) & (t.waitingOn = obj))
  1360. END;
  1361. TransferLock(hdr, t)
  1362. ELSE
  1363. hdr.lockedBy := NIL; hdr.count := 0
  1364. END
  1365. ELSE (* true condition found, transfer the lock *)
  1366. TransferLock(hdr, c);
  1367. t := NIL
  1368. END;
  1369. Machine.ReleaseObject(hdr.locked);
  1370. IF (c # NIL) OR (t # NIL) THEN
  1371. Machine.Acquire(Machine.Objects);
  1372. IF c # NIL THEN Enter(c) END;
  1373. IF t # NIL THEN Enter(t) END;
  1374. Machine.Release(Machine.Objects);
  1375. END;
  1376. Machine.ReleasePreemption;
  1377. END UnlockNoPriorityInv;
  1378. (* Unlock a protected object in case priority inversion handling is enabled. Machine.Objects lock is used. *)
  1379. PROCEDURE UnlockPriorityInv(obj: ProtectedObject);
  1380. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; t, c, r: Process; maxWaitingPrio: LONGINT;
  1381. BEGIN
  1382. IF Stats THEN Machine.AtomicInc(Nunlock) END;
  1383. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  1384. IF StrongChecks THEN
  1385. ASSERT(hdr IS Heaps.ProtRecBlock) (* protected object *)
  1386. END;
  1387. ASSERT(hdr.count = -1); (* exclusive locked *)
  1388. IF hdr.awaitingCond.head # NIL THEN (* evaluate the waiting conditions *)
  1389. (* we are holding the lock, so the queue can not change (to do: except in TerminateThis) *)
  1390. c := FindCondition(hdr.awaitingCond) (* interrupts should be on during this call *)
  1391. ELSE
  1392. c := NIL
  1393. END;
  1394. Machine.Acquire(Machine.Objects);
  1395. r := running[Machine.ID ()];
  1396. ASSERT(r # NIL);
  1397. IF hdr.lockedBy # r THEN
  1398. Machine.Release(Machine.Objects);
  1399. ASSERT(hdr.lockedBy = r)
  1400. END;
  1401. maxWaitingPrio := MaxPrio(hdr.waitingPriorities);
  1402. DEC(r.prioRequests[maxWaitingPrio]);
  1403. r.priority := MaxPrio(r.prioRequests);
  1404. IF c = NIL THEN (* no true condition found, check the lock queue *)
  1405. t := NIL;
  1406. GetMaxPrio(hdr.awaitingLock, t);
  1407. IF t = NIL THEN
  1408. hdr.lockedBy := NIL; hdr.count := 0
  1409. ELSE
  1410. IF StrongChecks THEN ASSERT((t.mode = AwaitingLock) & (t.waitingOn = obj)) END;
  1411. TransferLock(hdr, t)
  1412. END
  1413. ELSE (* true condition found, transfer the lock *)
  1414. TransferLock(hdr, c);
  1415. t := NIL
  1416. END;
  1417. IF (c # NIL) OR (t # NIL) THEN
  1418. IF c # NIL THEN Enter(c) END;
  1419. IF t # NIL THEN Enter(t) END;
  1420. END;
  1421. Machine.Release(Machine.Objects);
  1422. END UnlockPriorityInv;
  1423. (* Await a condition (kernel call). *)
  1424. (* There are two different procedures for locking a protected object in case of priority inverison handling enabled or disabled due to the different
  1425. locking strategies, i.e. there are no header locks in case of priority inversion handling. *)
  1426. PROCEDURE Await*(cond: Condition; slink: ADDRESS; obj: ProtectedObject; flags: SET);
  1427. BEGIN
  1428. IF HandlePriorityInv THEN
  1429. AwaitPriorityInv(cond, slink, obj, flags)
  1430. ELSE
  1431. AwaitNoPriorityInv(cond, slink, obj, flags)
  1432. END
  1433. END Await;
  1434. (* Await a condition if priority inversion handling is disabled. Header locks, preemption and Machine.Objects locks are used. *)
  1435. PROCEDURE AwaitNoPriorityInv(cond: Condition; slink: ADDRESS; obj: ProtectedObject; flags: SET);
  1436. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; r, c, t: Process; id: LONGINT;
  1437. BEGIN
  1438. IF Stats THEN Machine.AtomicInc(Nawait) END;
  1439. IF 1 IN flags THEN (* compiler did not generate IF *)
  1440. IF Stats THEN Machine.AtomicInc(NawaitNoIF) END;
  1441. IF cond(slink) THEN
  1442. IF Stats THEN Machine.AtomicInc(NawaitTrue) END;
  1443. RETURN (* condition already true *)
  1444. END
  1445. END;
  1446. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  1447. IF StrongChecks THEN
  1448. ASSERT(hdr IS Heaps.ProtRecBlock) (* protected object *)
  1449. END;
  1450. id := Machine.AcquirePreemption ();
  1451. Machine.AcquireObject(hdr.locked); (* must acquire object lock before other locks *)
  1452. r := running[id];
  1453. ASSERT(r # NIL);
  1454. IF hdr.lockedBy = r THEN (* current process holds exclusive lock *)
  1455. IF StrongChecks THEN ASSERT(hdr.count = -1) END; (* exclusive locked *)
  1456. IF hdr.awaitingCond.head # NIL THEN (* evaluate the waiting conditions *)
  1457. (* we are holding the lock, so the queue can not change (to do: except in TerminateThis) *)
  1458. c := FindCondition(hdr.awaitingCond) (* interrupts should be on during this call *)
  1459. ELSE
  1460. c := NIL
  1461. END;
  1462. IF c = NIL THEN
  1463. Get(hdr.awaitingLock, t);
  1464. IF t = NIL THEN (* none waiting - remove lock *)
  1465. hdr.count := 0; hdr.lockedBy := NIL;
  1466. ELSE (* transfer lock to first waiting process *)
  1467. IF StrongChecks THEN ASSERT(t.mode = AwaitingLock) END;
  1468. TransferLock(hdr, t)
  1469. END;
  1470. ELSE
  1471. TransferLock(hdr, c);
  1472. t := NIL
  1473. END;
  1474. ELSE (* no lock, or some other process may hold the lock, but that's the user's indaba (may be monotonic condition) *)
  1475. Machine.ReleaseObject(hdr.locked);
  1476. Machine.ReleasePreemption;
  1477. HALT(2204) (* await must be exclusive region *)
  1478. END;
  1479. Machine.Acquire(Machine.Objects); (* Put and SwitchTo must be protected *)
  1480. IF c # NIL THEN Enter(c) END;
  1481. IF t # NIL THEN Enter(t) END;
  1482. IF StrongChecks THEN ASSERT(r.waitingOn = NIL) END;
  1483. r.condition := cond; r.condFP := slink;
  1484. r.waitingOn := obj; r.mode := AwaitingCond;
  1485. Put(hdr.awaitingCond, r);
  1486. Machine.ReleaseObject(hdr.locked);
  1487. Machine.ReleasePreemption;
  1488. (* reschedule *)
  1489. SwitchToNew;
  1490. IF StrongChecks THEN
  1491. ASSERT(cond(slink));
  1492. ASSERT(hdr.lockedBy = r) (* lock held again *)
  1493. END
  1494. END AwaitNoPriorityInv;
  1495. (* Await a condition in case priority inversion handling is enabled. Machine.Objects lock is used. *)
  1496. PROCEDURE AwaitPriorityInv(cond: Condition; slink: ADDRESS; obj: ProtectedObject; flags: SET);
  1497. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; r, c, t: Process; id, maxWaitingPrio, prevMaxWaitingPrio: LONGINT;
  1498. BEGIN
  1499. IF Stats THEN Machine.AtomicInc(Nawait) END;
  1500. IF 1 IN flags THEN (* compiler did not generate IF *)
  1501. IF Stats THEN Machine.AtomicInc(NawaitNoIF) END;
  1502. IF cond(slink) THEN
  1503. IF Stats THEN Machine.AtomicInc(NawaitTrue) END;
  1504. RETURN (* condition already true *)
  1505. END
  1506. END;
  1507. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  1508. IF StrongChecks THEN
  1509. ASSERT(hdr IS Heaps.ProtRecBlock) (* protected object *)
  1510. END;
  1511. Machine.Acquire(Machine.Objects);
  1512. id := Machine.ID();
  1513. r := running[id];
  1514. ASSERT(r # NIL);
  1515. IF hdr.lockedBy = r THEN (* current process holds exclusive lock *)
  1516. IF StrongChecks THEN ASSERT(hdr.count = -1) END; (* exclusive locked *)
  1517. maxWaitingPrio := MaxPrio(hdr.waitingPriorities);
  1518. DEC(r.prioRequests[maxWaitingPrio]);
  1519. r.priority := MaxPrio(r.prioRequests);
  1520. IF hdr.awaitingCond.head # NIL THEN (* evaluate the waiting conditions *)
  1521. (* we are holding the lock, so the queue can not change (to do: except in TerminateThis) *)
  1522. c := FindCondition(hdr.awaitingCond) (* interrupts should be on during this call *)
  1523. ELSE
  1524. c := NIL
  1525. END;
  1526. IF c = NIL THEN
  1527. t := NIL;
  1528. GetMaxPrio(hdr.awaitingLock, t);
  1529. IF t = NIL THEN (* none waiting - remove lock *)
  1530. hdr.count := 0; hdr.lockedBy := NIL;
  1531. ELSE (* transfer lock to first waiting process *)
  1532. IF StrongChecks THEN ASSERT(t.mode = AwaitingLock) END;
  1533. TransferLock(hdr, t);
  1534. END;
  1535. ELSE (* true condition found, transfer the lock *)
  1536. TransferLock(hdr, c);
  1537. t := NIL;
  1538. END;
  1539. ELSE (* no lock, or some other process may hold the lock, but that's the user's indaba (may be monotonic condition) *)
  1540. Machine.Release(Machine.Objects);
  1541. HALT(2204) (* await must be exclusive region *)
  1542. END;
  1543. IF c # NIL THEN Enter(c) END;
  1544. IF t # NIL THEN Enter(t) END;
  1545. IF StrongChecks THEN ASSERT(r.waitingOn = NIL) END;
  1546. r.condition := cond; r.condFP := slink;
  1547. r.waitingOn := obj; r.mode := AwaitingCond;
  1548. IF hdr.lockedBy # NIL THEN
  1549. prevMaxWaitingPrio := MaxPrio(hdr.waitingPriorities);
  1550. INC(hdr.waitingPriorities[r.priority]);
  1551. IF r.priority > prevMaxWaitingPrio THEN PropagatePrio(hdr, prevMaxWaitingPrio, r.priority) END;
  1552. ELSE (* it may happen that hdr is not locked - in that case no priority propagation takes place *)
  1553. INC(hdr.waitingPriorities[r.priority])
  1554. END;
  1555. Put(hdr.awaitingCond, r);
  1556. (* reschedule *)
  1557. SwitchToNew;
  1558. IF StrongChecks THEN
  1559. ASSERT(cond(slink));
  1560. ASSERT(hdr.lockedBy = r) (* lock held again *)
  1561. END
  1562. END AwaitPriorityInv;
  1563. (** Update the state snapshot of the current process for GC. (for Processors) *)
  1564. PROCEDURE UpdateState;
  1565. VAR t: Process;
  1566. BEGIN (* interrupts off *)
  1567. Machine.Acquire(Machine.Objects);
  1568. t := running[Machine.ID ()];
  1569. ASSERT(t # NIL);
  1570. IF t # NIL THEN
  1571. t.state.PC := Machine.CurrentPC(); (* ug: required information for GC with meta data for stack inspection *)
  1572. t.state.SP := SYSTEM.GetStackPointer(); (* ug: not necessarily needed for GC *)
  1573. t.state.BP := SYSTEM.GetFramePointer(); (* ug: necessary information for GC with meta data for stack inspection *)
  1574. END;
  1575. Machine.Release(Machine.Objects)
  1576. END UpdateState;
  1577. (** Start executing user processes. Every processor calls this during initialization. *)
  1578. PROCEDURE Start*;
  1579. VAR id, ignored: LONGINT; idle: Idle; new: Process;
  1580. BEGIN (* running at kernel level (not preemptable) *)
  1581. ignored := Machine.AcquirePreemption();
  1582. id := Machine.ID (); (* preemption not enabled yet, because we are running at kernel level *)
  1583. NEW(idle); (* create process with MinPriority *)
  1584. Machine.Acquire(Machine.Objects);
  1585. Get(ready.q[MinPriority], new); (* can not use Select here, as it might return a preempted process *)
  1586. ASSERT(~(Preempted IN new.flags)); (* will at least get the Idle process just created *)
  1587. Machine.Release(Machine.Objects);
  1588. running[id] := new; (* schedule new process *)
  1589. perfTsc[id] := Machine.GetTimer();
  1590. new.mode := Running; new.procID := id;
  1591. Machine.FPURestoreMin(new.sse);
  1592. Machine.ReleasePreemption;
  1593. Machine.JumpToUserLevel(new.state.BP);
  1594. HALT(100); (* does never return here *)
  1595. END Start;
  1596. (* Initialize module. *)
  1597. PROCEDURE Init; (* can not use NEW *)
  1598. VAR
  1599. i: LONGINT;
  1600. BEGIN
  1601. ProcessorHLT := NIL;
  1602. maxReady := High; (* scan all queues at start *)
  1603. lowestAllowedPriority := Low; (* normal case, will be set to GCPriority if GC is running *)
  1604. gcBarrier := {};
  1605. FOR i := 0 TO Machine.MaxCPU - 1 DO running[i] := NIL END;
  1606. FOR i := 0 TO NumPriorities - 1 DO rootedProcesses[i] := NIL END;
  1607. FOR i := 0 TO NumIRQ-1 DO processingIRQ[i] := FALSE END;
  1608. nextProcessID := 0; Machine.ticks := 0;
  1609. traceProcess := NIL;
  1610. terminate := Terminate;
  1611. trap[0] := Halt;
  1612. trap[1] := HaltUnbreakable;
  1613. trapReturn[0] := HaltReturn;
  1614. trapReturn[1] := HaltUnbreakableReturn;
  1615. END Init;
  1616. PROCEDURE InitEventHandling;
  1617. VAR i: LONGINT; clock: Clock; (* realtimeClock: RealtimeClock; *)
  1618. BEGIN
  1619. FOR i := 0 TO NumIRQ-1 DO
  1620. interrupt[i].root := NIL; interrupt[i].process := NIL
  1621. END;
  1622. (* create normal event list *)
  1623. NEW(event); event.next := event; event.prev := event;
  1624. event.trigger := Machine.ticks + MAX(LONGINT) DIV 2;
  1625. (* create normal timer processes *)
  1626. timer := NIL; NEW(clock);
  1627. END InitEventHandling;
  1628. PROCEDURE InitGCHandling;
  1629. VAR finalizerCaller: FinalizerCaller;
  1630. BEGIN
  1631. gcProcess := NIL; NEW(gcActivity);
  1632. finalizerProcess := NIL; NEW(finalizerCaller);
  1633. END InitGCHandling;
  1634. PROCEDURE InitStats;
  1635. BEGIN
  1636. Nlock := 0; Nunlock := 0; Nawait := 0; NawaitNoIF := 0; NawaitTrue := 0;
  1637. Ncreate := 0; Nterminate := 0; Ncondition := 0; Ncondition1True := 0;
  1638. Ncondition2 := 0; Ncondition2True := 0;
  1639. Ntimeslice := 0; NtimesliceTaken := 0; NtimesliceNothing := 0;
  1640. NtimesliceIdle := 0; NtimesliceKernel := 0; NtimesliceV86 := 0; NtimesliceCritical := 0;
  1641. Npreempt := 0; NpreemptTaken := 0; NpreemptNothing := 0;
  1642. NpreemptKernel := 0; NpreemptV86 := 0; NpreemptCritical := 0;
  1643. Nenter := 0;
  1644. END InitStats;
  1645. PROCEDURE GCStatusFactory(): Heaps.GCStatus;
  1646. VAR gcStatusExt : GCStatusExt;
  1647. BEGIN
  1648. ASSERT(Heaps.gcStatus = NIL);
  1649. NEW(gcStatusExt);
  1650. RETURN gcStatusExt
  1651. END GCStatusFactory;
  1652. (** Return current user stack *)
  1653. PROCEDURE GetCurrentStack(VAR stack: Machine.Stack);
  1654. BEGIN
  1655. stack := running[Machine.ID()].stack;
  1656. END GetCurrentStack;
  1657. PROCEDURE InitPrioRequest;
  1658. VAR
  1659. i: LONGINT;
  1660. BEGIN
  1661. FOR i := 0 TO LEN(init.prioRequests) - 1 DO init.prioRequests[i] := 0 END;
  1662. END InitPrioRequest;
  1663. VAR
  1664. (* for compatibility and later extension *)
  1665. TraceProcessHook*: PROCEDURE (prcoess: Process; pc, bp: ADDRESS; stacklow, stackhigh: ADDRESS);
  1666. BEGIN
  1667. IF Stats THEN InitStats; END;
  1668. Init;
  1669. (* initialize memory management *)
  1670. Machine.UpdateState; (* for gc *)
  1671. Machine.getStack := GetCurrentStack;
  1672. Heaps.CollectGarbage(Modules.root); (* still in single-processor mode *)
  1673. (* now NEW can be used *)
  1674. NEW(ready); (* create the ready queues *)
  1675. Machine.InitInterrupts;
  1676. (*Machine.Start; initialize interrupts *)
  1677. InitEventHandling;
  1678. InitGCHandling;
  1679. Heaps.gcStatus := GCStatusFactory();
  1680. (* create a process for rest of init code, which runs at user level *)
  1681. entry := SYSTEM.GetFramePointer ();
  1682. SYSTEM.GET (entry+AddressSize, entry); (* return address into linker-generated call table *)
  1683. NEW(initObject);
  1684. NewProcess(SYSTEM.VAL (Body, entry-ReturnStackDisplacement), {Resistant}, initObject, init); (* create init process *)
  1685. init.priority := High;
  1686. init.staticPriority := init.priority;
  1687. (* initialize prioRequests for init process *)
  1688. InitPrioRequest;
  1689. INC(init.prioRequests[init.priority]);
  1690. Machine.Acquire(Machine.Objects);
  1691. init.id := -1; Enter(init); init := NIL;
  1692. Machine.Release(Machine.Objects);
  1693. Start (* start it *)
  1694. (* linker call table will end with a call to Terminate. So after executing all module bodies,
  1695. the init process will terminate and other processes created during init will continue running. *)
  1696. END Objects.
  1697. (*
  1698. 24.03.1998 pjm Started
  1699. 06.05.1998 pjm CreateProcess init process, page fault handler
  1700. 06.08.1998 pjm Moved exception interrupt handling here for current process
  1701. 17.08.1998 pjm FindRoots method
  1702. 02.10.1998 pjm Idle process
  1703. 06.11.1998 pjm snapshot
  1704. 25.03.1999 pjm Scope removed
  1705. 28.05.1999 pjm EventHandler object
  1706. 01.06.1999 pjm Fixed InterruptProcess lock error
  1707. 16.06.1999 pjm Flat IRQ priority model to avoid GC deadlock
  1708. 23.06.1999 pjm Flat IRQ priority experiment failed, rather do STI in FieldIRQ to avoid GC deadlock
  1709. 29.06.1999 pjm Timeout in EventHandler object
  1710. 13.01.2000 pjm Overed (Interrupt Objects, Event Handlers, Process ID, Process state, Process mode, Process stack, Await)
  1711. 17.10.2000 pjm Priorities
  1712. 22.10.2003 mib SSE2 extension
  1713. 24.10.2003 phk Priority inversion / cycle counters
  1714. 19.06.2007 ug Garbage Collector using meta data for stack inspection
  1715. *)
  1716. (*
  1717. Location Stack
  1718. Lock Current process
  1719. SwitchTo.A Current process
  1720. SwitchTo.B
  1721. *)