Unix.Machine.Mod 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. MODULE Machine; (** AUTHOR "pjm, G.F."; PURPOSE "Bootstrapping, configuration and machine interface"; *)
  2. IMPORT S := SYSTEM, Trace, Unix, Glue;
  3. CONST
  4. DefaultConfig = "Color 0 StackSize 128";
  5. #IF I386 THEN
  6. Version = "A2 Gen. 32-bit, ";
  7. DefaultObjectFileExtension* = ".GofU";
  8. #ELSIF AMD64 THEN
  9. Version = "A2 Gen. 64-bit, ";
  10. DefaultObjectFileExtension* = ".GofUu";
  11. #END
  12. Second* = 1000; (* frequency of ticks increments in Hz *)
  13. (** bits in features variable *)
  14. MTTR* = 12; MMX* = 23;
  15. AddressSize = SIZEOF(ADDRESS);
  16. StaticBlockSize = 8 * AddressSize; (* static heap block size *)
  17. MemBlockSize* = 64*1024*1024;
  18. TraceOutput* = 0; (* Trace output *)
  19. Memory* = 1; (*! Virtual memory management, stack and page allocation, not used in UnixAos *)
  20. Heaps* = 2; (* Storage allocation and Garbage collection *)
  21. Interrupts* = 3; (*! Interrupt handling, not used in UnixAos *)
  22. Modules* = 4; (* Module list *)
  23. Objects* = 5; (*! Ready queue, not used in UnixAos *)
  24. Processors* = 6; (*! Interprocessor interrupts, not used in UnixAos *)
  25. KernelLog* = 7; (* Atomic output *)
  26. X11* = 8; (* XWindows I/O *)
  27. MaxLocks* = 9; (* { <= 32 } *)
  28. MaxCPU* = 4;
  29. IsCooperative* = FALSE;
  30. TYPE
  31. Vendor* = ARRAY 13 OF CHAR;
  32. MemoryBlock* = POINTER {UNSAFE, UNTRACED} TO MemoryBlockDesc;
  33. MemoryBlockDesc* = RECORD
  34. next- : MemoryBlock;
  35. startAdr-: ADDRESS; (* sort key in linked list of memory blocks *)
  36. size-: SIZE;
  37. beginBlockAdr-, endBlockAdr-: ADDRESS
  38. END;
  39. (** processor state *)
  40. State* = RECORD
  41. PC*, BP*, SP*: ADDRESS
  42. END;
  43. VAR
  44. mtx : ARRAY MaxLocks OF Unix.Mutex_t;
  45. version-: ARRAY 64 OF CHAR; (** Aos version *)
  46. features-, features2 : SET;
  47. MMXSupport- : BOOLEAN;
  48. SSESupport- : BOOLEAN;
  49. SSE2Support- : BOOLEAN;
  50. SSE3Support- : BOOLEAN;
  51. SSSE3Support- : BOOLEAN;
  52. SSE41Support- : BOOLEAN;
  53. SSE42Support- : BOOLEAN;
  54. SSE5Support- : BOOLEAN;
  55. AVXSupport- : BOOLEAN;
  56. ticks-: LONGINT; (** timer ticks. Use Kernel.GetTicks() to read, don't write *)
  57. prioLow-, prioHigh-: LONGINT; (* permitted thread priorities *)
  58. fcr-: SET; (** default floating-point control register value (default rounding mode is towards -infinity, for ENTIER) *)
  59. mhz-: HUGEINT; (** clock rate of GetTimer in MHz, or 0 if not known *)
  60. gcThreshold-: SIZE;
  61. memBlockHead-{UNTRACED}, memBlockTail-{UNTRACED}: MemoryBlock; (* head and tail of sorted list of memory blocks *)
  62. config: ARRAY 2048 OF CHAR; (* config strings *)
  63. logname: ARRAY 32 OF CHAR;
  64. logfile: LONGINT;
  65. traceHeap: BOOLEAN;
  66. timer0 : HUGEINT;
  67. (** Return current processor ID (0 to MaxNum-1). *)
  68. PROCEDURE ID* (): LONGINT;
  69. BEGIN
  70. RETURN 0
  71. END ID;
  72. (**
  73. * Flush Data Cache for the specified virtual address range. If len is negative, flushes the whole cache.
  74. * This is used on some architecture to interact with DMA hardware (e.g. Ethernet and USB. It can be
  75. * left empty on Intel architecture.
  76. *)
  77. PROCEDURE FlushDCacheRange * (adr: ADDRESS; len: LONGINT);
  78. END FlushDCacheRange;
  79. (**
  80. * Invalidate Data Cache for the specified virtual address range. If len is negative, flushes the whole cache.
  81. * This is used on some architecture to interact with DMA hardware (e.g. Ethernet and USB. It can be
  82. * left empty on Intel architecture.
  83. *)
  84. PROCEDURE InvalidateDCacheRange * (adr: ADDRESS; len: LONGINT);
  85. END InvalidateDCacheRange;
  86. (**
  87. * Invalidate Instruction Cache for the specified virtual address range. If len is negative, flushes the whole cache.
  88. * This is used on some architecture to interact with DMA hardware (e.g. Ethernet and USB. It can be
  89. * left empty on Intel architecture.
  90. *)
  91. PROCEDURE InvalidateICacheRange * (adr: ADDRESS; len: LONGINT);
  92. END InvalidateICacheRange;
  93. (* insert given memory block in sorted list of memory blocks, sort key is startAdr field - called during GC *)
  94. PROCEDURE InsertMemoryBlock(memBlock: MemoryBlock);
  95. VAR cur {UNTRACED}, prev {UNTRACED}: MemoryBlock;
  96. BEGIN
  97. cur := memBlockHead;
  98. prev := NIL;
  99. WHILE (cur # NIL) & (ADDRESS OF cur^ < ADDRESS OF memBlock^) DO
  100. prev := cur;
  101. cur := cur.next
  102. END;
  103. IF prev = NIL THEN (* insert at head of list *)
  104. memBlock.next := memBlockHead;
  105. memBlockHead := memBlock
  106. ELSE (* insert in middle or at end of list *)
  107. prev.next := memBlock;
  108. memBlock.next := cur;
  109. END;
  110. IF cur = NIL THEN
  111. memBlockTail := memBlock
  112. END
  113. END InsertMemoryBlock;
  114. (* Free unused memory block - called during GC *)
  115. PROCEDURE FreeMemBlock*(memBlock: MemoryBlock);
  116. VAR cur {UNTRACED}, prev {UNTRACED}: MemoryBlock;
  117. BEGIN
  118. cur := memBlockHead;
  119. prev := NIL;
  120. WHILE (cur # NIL) & (cur # memBlock) DO
  121. prev := cur;
  122. cur := cur.next
  123. END;
  124. IF cur = memBlock THEN
  125. IF traceHeap THEN
  126. Trace.String( "Release memory block " ); Trace.Hex( memBlock.startAdr, -8 ); Trace.Ln
  127. END;
  128. IF prev = NIL THEN
  129. memBlockHead := cur.next
  130. ELSE
  131. prev.next := cur.next;
  132. IF cur.next = NIL THEN
  133. memBlockTail := prev
  134. END
  135. END;
  136. Unix.free( memBlock.startAdr )
  137. ELSE
  138. HALT(535) (* error in memory block management *)
  139. END;
  140. END FreeMemBlock;
  141. (* expand heap by allocating a new memory block *)
  142. PROCEDURE ExpandHeap*( dummy: LONGINT; size: SIZE; VAR memoryBlock: MemoryBlock; VAR beginBlockAdr, endBlockAdr: ADDRESS );
  143. VAR mBlock: MemoryBlock; alloc: SIZE; adr: ADDRESS;
  144. BEGIN
  145. ASSERT(SIZEOF(MemoryBlockDesc) <= StaticBlockSize); (* make sure MemoryBlock contents fits into one StaticBlock *)
  146. alloc := size + StaticBlockSize;
  147. IF alloc < MemBlockSize THEN alloc := MemBlockSize END;
  148. ASSERT((Unix.PageSize > StaticBlockSize) & (Unix.PageSize MOD StaticBlockSize = 0)); (* alignment to Unix.PageSize implies alignment to StaticBlockSize *)
  149. INC( alloc, (-alloc) MOD Unix.PageSize );
  150. IF Unix.posix_memalign( adr, Unix.PageSize, alloc ) # 0 THEN
  151. Unix.Perror( "Machine.ExpandHeap: posix_memalign" );
  152. beginBlockAdr := 0;
  153. endBlockAdr := 0;
  154. memoryBlock := NIL;
  155. ELSE
  156. IF Unix.mprotect( adr, alloc, 7 (* READ WRITE EXEC *) ) # 0 THEN
  157. Unix.Perror( "Machine.ExpandHeap: mprotect" )
  158. END;
  159. mBlock := adr;
  160. mBlock.next := NIL;
  161. mBlock.startAdr := adr;
  162. mBlock.size := alloc;
  163. beginBlockAdr := adr + StaticBlockSize;
  164. endBlockAdr := beginBlockAdr + alloc - StaticBlockSize;
  165. mBlock.beginBlockAdr := beginBlockAdr;
  166. mBlock.endBlockAdr := beginBlockAdr; (* block is still empty -- Heaps module will set the upper bound *)
  167. InsertMemoryBlock( mBlock );
  168. IF traceHeap THEN TraceHeap( mBlock ) END;
  169. memoryBlock := mBlock;
  170. END
  171. END ExpandHeap;
  172. (* Set memory block end address *)
  173. PROCEDURE SetMemoryBlockEndAddress*(memBlock: MemoryBlock; endBlockAdr: ADDRESS);
  174. BEGIN
  175. ASSERT(endBlockAdr >= memBlock.beginBlockAdr);
  176. memBlock.endBlockAdr := endBlockAdr
  177. END SetMemoryBlockEndAddress;
  178. PROCEDURE TraceHeap( new: MemoryBlock );
  179. VAR cur{UNTRACED}: MemoryBlock;
  180. BEGIN
  181. Trace.Ln;
  182. Trace.String( "Heap expanded" ); Trace.Ln;
  183. Trace.String("Static Heap: "); Trace.Hex(Glue.baseAdr, -8); Trace.String(" - "); Trace.Hex(Glue.endAdr, -8);
  184. Trace.Ln;
  185. cur := memBlockHead;
  186. WHILE cur # NIL DO
  187. Trace.Hex( cur.startAdr, -8 ); Trace.String( " " ); Trace.Hex( cur.startAdr + cur.size, -8 );
  188. IF cur = new THEN Trace.String( " (new)" ) END;
  189. Trace.Ln;
  190. cur := cur.next
  191. END
  192. END TraceHeap;
  193. (** Get first memory block and first free address, the first free address is identical to memBlockHead.endBlockAdr *)
  194. PROCEDURE GetStaticHeap*(VAR beginBlockAdr, endBlockAdr, freeBlockAdr: ADDRESS);
  195. BEGIN
  196. beginBlockAdr := NIL; endBlockAdr := NIL; freeBlockAdr := NIL;
  197. END GetStaticHeap;
  198. (* returns if an address is a currently allocated heap address *)
  199. PROCEDURE ValidHeapAddress*( p: ADDRESS ): BOOLEAN;
  200. VAR mb: MemoryBlock;
  201. BEGIN
  202. IF (p>=Glue.baseAdr) & (p<=Glue.endAdr) THEN RETURN TRUE END;
  203. mb := memBlockHead;
  204. WHILE mb # NIL DO
  205. IF (p >= mb.beginBlockAdr) & (p <= mb.endBlockAdr) THEN RETURN TRUE END;
  206. mb := mb.next;
  207. END;
  208. RETURN FALSE;
  209. END ValidHeapAddress;
  210. (** Return information on free memory in Kbytes. *)
  211. PROCEDURE GetFreeK*(VAR total, lowFree, highFree: SIZE);
  212. BEGIN
  213. (*! meaningless in Unix port, for interface compatibility only *)
  214. total := 0;
  215. lowFree := 0;
  216. highFree := 0
  217. END GetFreeK;
  218. (** Fill "size" bytes at "destAdr" with "filler". "size" must be multiple of 4. *)
  219. PROCEDURE Fill32* (destAdr: ADDRESS; size: SIZE; filler: LONGINT);
  220. CODE
  221. #IF I386 THEN
  222. MOV EDI, [EBP+destAdr]
  223. MOV ECX, [EBP+size]
  224. MOV EAX, [EBP+filler]
  225. TEST ECX, 3
  226. JZ ok
  227. PUSH 8 ; ASSERT failure
  228. INT 3
  229. ok:
  230. SHR ECX, 2
  231. CLD
  232. REP STOSD
  233. #ELSIF AMD64 THEN
  234. MOV RDI, [RBP + destAdr]
  235. MOV RCX, [RBP + size]
  236. MOV EAX, [RBP + filler]
  237. TEST RCX, 3
  238. JZ ok
  239. PUSH 8 ; ASSERT failure
  240. INT 3
  241. ok:
  242. SHR RCX, 2
  243. CLD
  244. REP STOSD
  245. #ELSE
  246. unimpemented
  247. #END
  248. END Fill32;
  249. PROCEDURE Portin8*(port: LONGINT; VAR val: CHAR);
  250. END Portin8;
  251. PROCEDURE Portin16*(port: LONGINT; VAR val: INTEGER);
  252. END Portin16;
  253. PROCEDURE Portin32*(port: LONGINT; VAR val: LONGINT);
  254. END Portin32;
  255. PROCEDURE Portout8*(port: LONGINT; val: CHAR);
  256. END Portout8;
  257. PROCEDURE Portout16*(port: LONGINT; val: INTEGER);
  258. END Portout16;
  259. PROCEDURE Portout32*(port: LONGINT; val: LONGINT);
  260. END Portout32;
  261. (** -- Atomic operations -- *)
  262. (** Atomic INC(x). *)
  263. PROCEDURE -AtomicInc*(VAR x: LONGINT);
  264. CODE
  265. #IF I386 THEN
  266. POP EAX
  267. LOCK
  268. INC DWORD [EAX]
  269. #ELSIF AMD64 THEN
  270. POP RAX
  271. LOCK
  272. INC DWORD [RAX]
  273. #ELSE
  274. unimplemented
  275. #END
  276. END AtomicInc;
  277. (** Atomic DEC(x). *)
  278. PROCEDURE -AtomicDec*(VAR x: LONGINT);
  279. CODE
  280. #IF I386 THEN
  281. POP EAX
  282. LOCK
  283. DEC DWORD [EAX]
  284. #ELSIF AMD64 THEN
  285. POP RAX
  286. LOCK
  287. DEC DWORD [RAX]
  288. #ELSE
  289. unimplemented
  290. #END
  291. END AtomicDec;
  292. (** Atomic INC(x, y). *)
  293. PROCEDURE -AtomicAdd*(VAR x: LONGINT; y: LONGINT);
  294. CODE
  295. #IF I386 THEN
  296. POP EBX
  297. POP EAX
  298. LOCK
  299. ADD DWORD [EAX], EBX
  300. #ELSIF AMD64 THEN
  301. POP EBX
  302. POP RAX
  303. LOCK
  304. ADD DWORD [RAX], EBX
  305. #ELSE
  306. unimplemented
  307. #END
  308. END AtomicAdd;
  309. (** Atomic EXCL. *)
  310. PROCEDURE AtomicExcl* (VAR s: SET; bit: LONGINT);
  311. CODE
  312. #IF I386 THEN
  313. MOV EAX, [EBP+bit]
  314. MOV EBX, [EBP+s]
  315. LOCK
  316. BTR [EBX], EAX
  317. #ELSIF AMD64 THEN
  318. MOV EAX, [RBP + bit]
  319. MOV RBX, [RBP + s]
  320. LOCK
  321. BTR [RBX], EAX
  322. #ELSE
  323. unimplemented
  324. #END
  325. END AtomicExcl;
  326. (** Atomic test-and-set. Set x = TRUE and return old value of x. *)
  327. PROCEDURE -AtomicTestSet*(VAR x: BOOLEAN): BOOLEAN;
  328. CODE
  329. #IF I386 THEN
  330. POP EBX
  331. MOV AL, 1
  332. XCHG [EBX], AL
  333. #ELSIF AMD64 THEN
  334. POP RBX
  335. MOV AL, 1
  336. XCHG [RBX], AL
  337. #ELSE
  338. unimplemented
  339. #END
  340. END AtomicTestSet;
  341. (* Atomic compare-and-swap. Set x = new if x = old and return old value of x *)
  342. PROCEDURE -AtomicCAS* (VAR x: LONGINT; old, new: LONGINT): LONGINT;
  343. CODE
  344. #IF I386 THEN
  345. POP EBX ; new
  346. POP EAX ; old
  347. POP ECX ; address of x
  348. LOCK CMPXCHG [ECX], EBX; atomicly compare x with old and set it to new if equal
  349. #ELSIF AMD64 THEN
  350. POP EBX ; new
  351. POP EAX ; old
  352. POP RCX ; address of x
  353. LOCK CMPXCHG [RCX], EBX ; atomicly compare x with old and set it to new if equal
  354. #ELSE
  355. unimplemented
  356. #END
  357. END AtomicCAS;
  358. (* Return current instruction pointer *)
  359. PROCEDURE CurrentPC* (): ADDRESS;
  360. CODE
  361. #IF I386 THEN
  362. MOV EAX, [EBP+4]
  363. #ELSIF AMD64 THEN
  364. MOV RAX, [RBP + 8]
  365. #ELSE
  366. unimplemented
  367. #END
  368. END CurrentPC;
  369. PROCEDURE -GetTimer* (): HUGEINT;
  370. CODE
  371. #IF I386 THEN
  372. RDTSC ; set EDX:EAX
  373. #ELSIF AMD64 THEN
  374. XOR RAX, RAX
  375. RDTSC ; set EDX:EAX
  376. SHL RDX, 32
  377. OR RAX, RDX
  378. #END
  379. END GetTimer;
  380. (** -- Configuration and bootstrapping -- *)
  381. (** Return the value of the configuration string specified by parameter name in parameter val. Returns val = "" if the string was not found, or has an empty value. *)
  382. PROCEDURE GetConfig* (CONST name: ARRAY OF CHAR; VAR val: ARRAY OF CHAR);
  383. VAR i, src: LONGINT; ch: CHAR;
  384. BEGIN
  385. ASSERT (name[0] # "="); (* no longer supported, use GetInit instead *)
  386. src := -1; val := "";
  387. LOOP
  388. REPEAT
  389. INC( src ); ch := config[src];
  390. IF ch = 0X THEN EXIT END;
  391. UNTIL ch > ' ';
  392. i := 0;
  393. LOOP
  394. ch := config[src];
  395. IF (ch # name[i]) OR (name[i] = 0X) THEN EXIT END;
  396. INC (i); INC (src)
  397. END;
  398. IF (ch <= ' ') & (name[i] = 0X) THEN (* found *)
  399. i := 0;
  400. REPEAT
  401. INC (src); ch := config[src]; val[i] := ch; INC (i);
  402. IF i = LEN(val) THEN val[i - 1] := 0X; RETURN END (* val too short *)
  403. UNTIL ch <= ' ';
  404. IF ch = ' ' THEN val[i -1] := 0X END;
  405. RETURN
  406. ELSE
  407. WHILE ch > ' ' DO (* skip to end of name *)
  408. INC (src); ch := config[src]
  409. END;
  410. INC (src);
  411. REPEAT (* skip to end of value *)
  412. ch := config[src]; INC (src)
  413. UNTIL ch <= ' '
  414. END
  415. END;
  416. IF (name = "ObjectFileExtension") & (val = "") THEN
  417. val := DefaultObjectFileExtension
  418. END;
  419. END GetConfig;
  420. (** Convert a string to an integer. Parameter i specifies where in the string scanning should begin (usually 0 in the first call). Scanning stops at the first non-valid character, and i returns the updated position. Parameter s is the string to be scanned. The value is returned as result, or 0 if not valid. Syntax: number = ["-"] digit {digit} ["H" | "h"] . digit = "0" | ... "9" | "A" .. "F" | "a" .. "f" . If the number contains any hexdecimal letter, or if it ends in "H" or "h", it is interpreted as hexadecimal. *)
  421. PROCEDURE StrToInt* (VAR i: LONGINT; CONST s: ARRAY OF CHAR): LONGINT;
  422. VAR vd, vh, sgn, d: LONGINT; hex: BOOLEAN;
  423. BEGIN
  424. vd := 0; vh := 0; hex := FALSE;
  425. IF s[i] = "-" THEN sgn := -1; INC (i) ELSE sgn := 1 END;
  426. LOOP
  427. IF (s[i] >= "0") & (s[i] <= "9") THEN d := ORD (s[i])-ORD ("0")
  428. ELSIF (CAP (s[i]) >= "A") & (CAP (s[i]) <= "F") THEN d := ORD (CAP (s[i]))-ORD ("A") + 10; hex := TRUE
  429. ELSE EXIT
  430. END;
  431. vd := 10*vd + d; vh := 16*vh + d;
  432. INC (i)
  433. END;
  434. IF CAP (s[i]) = "H" THEN hex := TRUE; INC (i) END; (* optional H *)
  435. IF hex THEN vd := vh END;
  436. RETURN sgn * vd
  437. END StrToInt;
  438. (* function returning the number of processors that are available to Aos *)
  439. PROCEDURE NumberOfProcessors*( ): LONGINT;
  440. VAR res: LONGINT;
  441. BEGIN
  442. res := Unix.getnprocs();
  443. RETURN res;
  444. END NumberOfProcessors;
  445. (*! non portable code, for native Aos only *)
  446. PROCEDURE SetNumberOfProcessors*( num: LONGINT );
  447. BEGIN
  448. (* numberOfProcessors := num; *)
  449. END SetNumberOfProcessors;
  450. (* function for changing byte order *)
  451. PROCEDURE ChangeByteOrder* (n: LONGINT): LONGINT;
  452. CODE
  453. #IF I386 THEN
  454. MOV EAX, [EBP+n] ; load n in eax
  455. BSWAP EAX ; swap byte order
  456. #ELSIF AMD64 THEN
  457. MOV EAX, [RBP+n] ; load n in eax
  458. BSWAP EAX ; swap byte order
  459. #ELSE
  460. unimplemented
  461. #END
  462. END ChangeByteOrder;
  463. (* Send and print character *)
  464. PROCEDURE TraceChar *(c: CHAR);
  465. BEGIN
  466. Trace.Char( c )
  467. END TraceChar;
  468. (** CPU identification *)
  469. PROCEDURE CPUID*( VAR vendor: Vendor; VAR version: LONGINT; VAR features1,features2: SET );
  470. CODE
  471. #IF I386 THEN
  472. PUSH ECX
  473. MOV EAX, 0
  474. CPUID
  475. CMP EAX, 0
  476. JNE ok
  477. MOV ESI, [EBP+vendor]
  478. MOV [ESI], AL ; AL = 0
  479. MOV ESI, [EBP+version]
  480. MOV [ESI], EAX ; EAX = 0
  481. MOV ESI, [EBP+features1]
  482. MOV [ESI], EAX
  483. MOV ESI, [EBP+features2]
  484. MOV [ESI], EAX
  485. JMP end
  486. ok:
  487. MOV ESI, [EBP+vendor]
  488. MOV [ESI], EBX
  489. MOV [ESI+4], EDX
  490. MOV [ESI+8], ECX
  491. MOV BYTE [ESI+12], 0
  492. MOV EAX, 1
  493. CPUID
  494. MOV ESI, [EBP+version]
  495. MOV [ESI], EAX
  496. MOV ESI, [EBP+features1]
  497. MOV [ESI], EDX
  498. MOV ESI, [EBP+features2]
  499. MOV [ESI], ECX
  500. end:
  501. POP ECX
  502. #ELSIF AMD64 THEN
  503. PUSH RCX
  504. MOV EAX, 0
  505. CPUID
  506. CMP EAX, 0
  507. JNE ok
  508. MOV RSI, [RBP+vendor]
  509. MOV [RSI], AL ; AL = 0
  510. MOV RSI, [RBP+version]
  511. MOV [RSI], EAX ; EAX = 0
  512. MOV RSI, [RBP+features1]
  513. MOV [RSI], EAX
  514. MOV RSI, [RBP+features2]
  515. MOV [RSI], EAX
  516. JMP end
  517. ok:
  518. MOV RSI, [RBP+vendor]
  519. MOV [RSI], EBX
  520. MOV [RSI+4], EDX
  521. MOV [RSI+8], ECX
  522. MOV BYTE [RSI+12], 0
  523. MOV EAX, 1
  524. CPUID
  525. MOV RSI, [RBP+version]
  526. MOV [RSI], EAX
  527. MOV RSI, [RBP+features1]
  528. MOV [RSI], EDX
  529. MOV RSI, [RBP+features2]
  530. MOV [RSI], RCX
  531. end:
  532. POP RCX
  533. #ELSE
  534. unimplemented
  535. #END
  536. END CPUID;
  537. (* If the CPUID instruction is supported, the ID flag (bit 21) of the EFLAGS register is r/w *)
  538. PROCEDURE CpuIdSupported( ) : BOOLEAN;
  539. CODE
  540. #IF I386 THEN
  541. PUSHFD ; save EFLAGS
  542. POP EAX ; store EFLAGS in EAX
  543. MOV EBX, EAX ; save EBX for later testing
  544. XOR EAX, 00200000H ; toggle bit 21
  545. PUSH EAX ; push to stack
  546. POPFD ; save changed EAX to EFLAGS
  547. PUSHFD ; push EFLAGS to TOS
  548. POP EAX ; store EFLAGS in EAX
  549. CMP EAX, EBX ; see if bit 21 has changed
  550. SETNE AL; ; return TRUE if bit 21 has changed, FALSE otherwise
  551. #ELSIF AMD64 THEN
  552. PUSHFQ ; save RFLAGS
  553. POP RAX ; store RFLAGS in EAX
  554. MOV RBX, RAX ; save RBX for later testing
  555. XOR RAX, 00200000H ; toggle bit 21
  556. PUSH RAX ; push to stack
  557. POPFQ ; save changed EAX to EFLAGS
  558. PUSHFQ ; push EFLAGS to TOS
  559. POP RAX ; store EFLAGS in EAX
  560. CMP RAX, RBX ; see if bit 21 has changed
  561. SETNE AL; ; return TRUE if bit 21 has changed, FALSE otherwise
  562. #ELSE
  563. unimplemented
  564. #END
  565. END CpuIdSupported;
  566. (* setup MMX, SSE and SSE2..SSE5 and AVX extension *)
  567. PROCEDURE SetupSSE2Ext;
  568. CONST
  569. MMXFlag=23;(*IN features from EBX*)
  570. FXSRFlag = 24;
  571. SSEFlag = 25;
  572. SSE2Flag = 26;
  573. SSE3Flag = 0; (*IN features2 from ECX*) (*PH 04/11*)
  574. SSSE3Flag =9;
  575. SSE41Flag =19;
  576. SSE42Flag =20;
  577. SSE5Flag = 11;
  578. AVXFlag = 28;
  579. BEGIN
  580. MMXSupport := MMXFlag IN features;
  581. SSESupport := SSEFlag IN features;
  582. SSE2Support := SSESupport & (SSE2Flag IN features);
  583. SSE3Support := SSE2Support & (SSE3Flag IN features2);
  584. SSSE3Support := SSE3Support & (SSSE3Flag IN features2); (* PH 04/11*)
  585. SSE41Support := SSE3Support & (SSE41Flag IN features2);
  586. SSE42Support := SSE3Support & (SSE42Flag IN features2);
  587. SSE5Support := SSE3Support & (SSE5Flag IN features2);
  588. AVXSupport := SSE3Support & (AVXFlag IN features2);
  589. IF SSESupport & (FXSRFlag IN features) THEN
  590. (* InitSSE(); *) (*! not privileged mode in Windows and Unix, not allowed *)
  591. END;
  592. END SetupSSE2Ext;
  593. (** -- Processor initialization -- *)
  594. PROCEDURE -SetFCR( s: SET );
  595. CODE
  596. #IF I386 THEN
  597. FLDCW [ESP] ; parameter s
  598. POP EAX
  599. #ELSIF AMD64 THEN
  600. FLDCW WORD [RSP] ; parameter s
  601. POP RAX
  602. #ELSE
  603. unimplemented
  604. #END
  605. END SetFCR;
  606. PROCEDURE -FCR( ): SET;
  607. CODE
  608. #IF I386 THEN
  609. PUSH 0
  610. FNSTCW [ESP]
  611. FWAIT
  612. POP EAX
  613. #ELSIF AMD64 THEN
  614. PUSH 0
  615. FNSTCW WORD [RSP]
  616. FWAIT
  617. POP RAX
  618. #ELSE
  619. unimplemented
  620. #END
  621. END FCR;
  622. PROCEDURE -InitFPU;
  623. CODE
  624. #IF I386 THEN
  625. FNINIT
  626. #ELSIF AMD64 THEN
  627. FNINIT
  628. #ELSE
  629. unimplemented
  630. #END
  631. END InitFPU;
  632. (** Setup FPU control word of current processor. *)
  633. PROCEDURE SetupFPU*;
  634. BEGIN
  635. InitFPU; SetFCR( fcr )
  636. END SetupFPU;
  637. (* Initialize locks. *)
  638. PROCEDURE InitLocks;
  639. VAR i: LONGINT;
  640. BEGIN
  641. i := 0;
  642. WHILE i < MaxLocks DO mtx[i] := Unix.NewMtx( ); INC( i ) END;
  643. END InitLocks;
  644. PROCEDURE CleanupLocks*;
  645. VAR i: LONGINT;
  646. BEGIN
  647. i := 0;
  648. WHILE i < MaxLocks DO Unix.MtxDestroy( mtx[i] ); INC( i ) END;
  649. END CleanupLocks;
  650. (** Acquire a spin-lock. *)
  651. PROCEDURE Acquire*( level: LONGINT ); (* non reentrant lock *)
  652. BEGIN
  653. Unix.MtxLock( mtx[level] );
  654. END Acquire;
  655. (** Release a spin-lock. *)
  656. PROCEDURE Release*( level: LONGINT );
  657. BEGIN
  658. Unix.MtxUnlock( mtx[level] );
  659. END Release;
  660. PROCEDURE Shutdown*( reboot: BOOLEAN );
  661. VAR r: LONGINT; logstat: Unix.Status;
  662. BEGIN
  663. IF logfile > 0 THEN
  664. r := Unix.fstat( logfile, logstat );
  665. r := Unix.close( logfile );
  666. IF logstat.size = 0 THEN r := Unix.unlink( ADDRESSOF( logname) ) END;
  667. END;
  668. IF reboot THEN Unix.exit( 0 ) ELSE Unix.exit( 1 ) END;
  669. END Shutdown;
  670. (* Set machine-dependent parameter gcThreshold *)
  671. PROCEDURE SetGCParams*;
  672. BEGIN
  673. gcThreshold := 10*1024*1024; (* 10 MB *)
  674. END SetGCParams;
  675. PROCEDURE InitConfig;
  676. VAR a: ADDRESS; i: LONGINT; c: CHAR;
  677. BEGIN
  678. a := Unix.getenv( ADDRESSOF( "AOSCONFIG" ) );
  679. IF a = 0 THEN config := DefaultConfig
  680. ELSE
  681. REPEAT
  682. S.GET( a, c ); INC( a ); config[i] := c; INC( i )
  683. UNTIL c = 0X
  684. END
  685. END InitConfig;
  686. PROCEDURE UpdateTicks*;
  687. BEGIN
  688. ticks := SHORT( (GetTimer() - timer0) DIV (mhz * 1000) );
  689. END UpdateTicks;
  690. PROCEDURE InitThreads;
  691. VAR res: BOOLEAN;
  692. BEGIN
  693. res := Unix.ThrInitialize( prioLow, prioHigh );
  694. IF ~res THEN
  695. Trace.StringLn( "Machine.InitThreads: no threads support in boot environment. teminating" );
  696. Unix.exit( 1 )
  697. END;
  698. IF Glue.debug # {} THEN
  699. Trace.String( "Threads initialized, priorities low, high: " );
  700. Trace.Int( prioLow, 0 ); Trace.String( ", " ); Trace.Int( prioHigh, 0 );
  701. Trace.Ln
  702. END
  703. END InitThreads;
  704. PROCEDURE CPUSpeed;
  705. VAR t0, t1: HUGEINT;
  706. BEGIN
  707. t0 := GetTimer(); Unix.ThrSleep( 100 ); t1 := GetTimer();
  708. mhz := (t1 - t0) DIV 100000;
  709. IF Glue.debug # {} THEN
  710. Trace.String( "CPU speed: ~" ); Trace.Int( SHORT( mhz ), 0); Trace.String( " MHz" ); Trace.Ln
  711. END
  712. END CPUSpeed;
  713. PROCEDURE Log( c: CHAR );
  714. VAR ignore: SIZE;
  715. BEGIN
  716. ignore := Unix.write( 1, ADDRESSOF( c ), 1 );
  717. ignore := Unix.write( logfile, ADDRESSOF( c ), 1 );
  718. END Log;
  719. PROCEDURE LogFileOnly( c: CHAR );
  720. VAR ignore: SIZE;
  721. BEGIN
  722. ignore := Unix.write( logfile, ADDRESSOF( c ), 1 );
  723. END LogFileOnly;
  724. PROCEDURE InitLog;
  725. VAR pid, i: LONGINT;
  726. BEGIN
  727. IF logfile > 0 THEN RETURN END;
  728. logname := "AOS.xxxxx.Log";
  729. pid := Unix.getpid(); i := 8;
  730. REPEAT
  731. logname[i] := CHR( pid MOD 10 + ORD( '0' ) ); DEC( i );
  732. pid := pid DIV 10;
  733. UNTIL i = 3;
  734. logfile := Unix.open( ADDRESSOF( logname ), Unix.rdwr + Unix.creat + Unix.trunc, Unix.rwrwr );
  735. END InitLog;
  736. PROCEDURE SilentLog*;
  737. BEGIN
  738. InitLog;
  739. Trace.Char := LogFileOnly
  740. END SilentLog;
  741. PROCEDURE VerboseLog*;
  742. BEGIN
  743. InitLog;
  744. Trace.Char := Log
  745. END VerboseLog;
  746. PROCEDURE Append( VAR a: ARRAY OF CHAR; CONST this: ARRAY OF CHAR );
  747. VAR i, j: LONGINT;
  748. BEGIN
  749. i := 0; j := 0;
  750. WHILE a[i] # 0X DO INC( i ) END;
  751. WHILE (i < LEN( a ) - 1) & (this[j] # 0X) DO a[i] := this[j]; INC( i ); INC( j ) END;
  752. a[i] := 0X
  753. END Append;
  754. PROCEDURE Init;
  755. VAR vendor: Vendor; ver: LONGINT;
  756. BEGIN
  757. COPY( Unix.Version, version ); Append( version, Version ); Append(version, S.Date);
  758. timer0 := GetTimer( ); ticks := 0;
  759. InitThreads;
  760. InitLocks;
  761. traceHeap := 1 IN Glue.debug;
  762. InitConfig;
  763. CPUSpeed;
  764. IF CpuIdSupported() THEN
  765. CPUID( vendor, ver, features, features2 ); SetupSSE2Ext
  766. END;
  767. fcr := (FCR() - {0,2,3,10,11}) + {0..5,8,9}; (* default FCR RC=00B *)
  768. END Init;
  769. PROCEDURE {INITIAL} Init0*;
  770. BEGIN
  771. Init;
  772. END Init0;
  773. END Machine.
  774. (*
  775. 03.03.1998 pjm First version
  776. 30.06.1999 pjm ProcessorID moved to AosProcessor
  777. *)
  778. (**
  779. Notes
  780. This module defines an interface to the boot environment of the system. The facilities provided here are only intended for the lowest levels of the system, and should never be directly imported by user modules (exceptions are noted below). They are highly specific to the system hardware and firmware architecture.
  781. Typically a machine has some type of firmware that performs initial testing and setup of the system. The firmware initiates the operating system bootstrap loader, which loads the boot file. This module is the first module in the statically linked boot file that gets control.
  782. There are two more-or-less general procedures in this module: GetConfig and StrToInt. GetConfig is used to query low-level system settings, e.g., the location of the boot file system. StrToInt is a utility procedure that parses numeric strings.
  783. Config strings:
  784. ExtMemSize Specifies size of extended memory (above 1MB) in MB. This value is not checked for validity. Setting it false may cause the system to fail, possible after running for some time. The memory size is usually detected automatically, but if the detection does not work for some reason, or if you want to limit the amount of memory detected, this string can be set. For example, if the machine has 64MB of memory, this value can be set as ExtMemSize="63".
  785. *)