rtdrv.s 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. # rtdrv.s -- call runtime entries, with known arguments, and report what
  2. # happened. Assembled by tests/rt_exec.py into a complete .COM image.
  3. #
  4. # This replaces the Unicorn harness. The expectations did not change; only the
  5. # machine did. Everything here exists because qemu, unlike an emulator with a
  6. # Python API, gives you one thing: a serial port. So a check has to be
  7. # expressed as a program that reports on itself.
  8. #
  9. # The report is a framed record on the serial port:
  10. #
  11. # 01 <case index, 2 hex digits> 02 <whatever the entries printed> 03
  12. # <dumplen bytes at dumpaddr, 2 hex digits each> 04
  13. #
  14. # The four control bytes occur in no expected output, and rt_exec.py REFUSES to
  15. # run a case whose expectation contains one -- so a marker can never be mistaken
  16. # for output, and a driver that emitted nothing is a parse failure rather than
  17. # a silent pass.
  18. #
  19. # Why one qemu boot per check instead of one boot for all of them: the runtime
  20. # keeps state that outlives a call -- the one-character pushback slot, and the
  21. # input cursor the boot sector's INT 21h shim owns. One boot for everything
  22. # would make each check depend on the ones before it, so a check could fail
  23. # because of its neighbour and a green run would prove less than it appears to.
  24. # A boot costs about a tenth of a second and 35 of them cost under four, which
  25. # buys every check a machine that has provably never executed anything. (The
  26. # input descriptor is still re-armed on every boot, because it lives in the
  27. # image and the image is the same bytes each time.)
  28. #
  29. # Two things about the 8086 shape the code below, and both were found by
  30. # running it rather than by reading it:
  31. #
  32. # * Every entry preserves BP and SP and NO other register. So the cursor
  33. # into the case record is BP: a loop counter in CX or DI would be destroyed
  34. # by the first call. The entry's own BP frame is pushed and restored, so
  35. # our BP comes back.
  36. #
  37. # * `movb n(%bp), %cl' leaves the HIGH byte of CX exactly as it was. Reading
  38. # the input length that way and then writing CX to INLEN handed the boot
  39. # sector's INT 21h shim a 16-bit length with an undefined top half. The
  40. # 8086 has no memory-to-memory move and no zero-extending byte load, so the
  41. # high half has to be cleared by hand, every time.
  42. #
  43. # * `dec' sets the sign flag from the RESULT, so `decw cnt / js done' runs the
  44. # body for cnt = 1 (1 -> 0, SF clear) and stops on the wrap (0 -> FFFF).
  45. # It reads like an off-by-one and is not; the comment at the loop says so
  46. # again because the next person will wonder.
  47. #
  48. # The image is assembled in ONE piece, here, so every address is a label or an
  49. # .org and nothing has to be agreed with Python in two places. What Python does
  50. # have to agree about -- the runtime's base, and the input descriptor -- is
  51. # checked rather than trusted: it assembles this file and then asserts that the
  52. # blob it got from RtProbe really is at RT_BASE in the bytes that came out.
  53. .set LOAD_BIAS, 0x0100 # a .COM lives at CS:0100 (that is the PSP)
  54. .set SOH, 0x01
  55. .set STX, 0x02
  56. .set ETX, 0x03
  57. .set EOT, 0x04
  58. # Restated from tests/exec/bootcom.s's layout block ON PURPOSE. A harness that
  59. # asks the boot sector where its input buffer is proves only that the two agree
  60. # with each other; two independent statements of the same layout have to be
  61. # written down, and a disagreement is a loud failure rather than a program that
  62. # is fed nothing. (Same reasoning, and the same lesson, as the hard-coded
  63. # RT_SZ that run_com_tests.sh used to carry: the descriptor has to be where the
  64. # shim looks, and the only way to be sure is to have written both down and
  65. # checked.)
  66. .set INLEN, 0x2000 # word: how many input bytes follow
  67. .set INCUR, 0x2002 # word: index into INBUF, NOT an address
  68. .set INBUF, 0x2004 # the bytes themselves
  69. # The case record, as rt_exec.py writes it. These offsets are the contract; the
  70. # two places that read them (this file, and case_bytes() over there) are
  71. # separate statements of it on purpose, and the cases only pass if both are
  72. # right. SLOT0 is the one that was wrong the first time round.
  73. .set OFF_DUMPLEN, 0 # word how many bytes to report
  74. .set OFF_DUMPADR, 2 # word image offset to report them from
  75. .set OFF_INLEN, 4 # byte bytes of input to hand the shim
  76. .set OFF_INBUF, 5 # byte inbuf[8]
  77. .set OFF_CASE, 13 # byte the index printed in the header
  78. .set OFF_NCALLS, 14 # word how many call slots follow
  79. .set SLOT0, 16 # call slots, 12 bytes each:
  80. # +0 word entry, +2 word arg A,
  81. # +4 byte flags, +5 pad,
  82. # +6 word arg B, +8 word CX,
  83. # +10 pad
  84. .set SLOT_SZ, 12
  85. .set CASE_SZ, 64 # 16 + 4 slots
  86. # The image layout. RT_BASE is where the runtime blob goes; everything else is
  87. # above 0x0800 so that a runtime which grew by a few hundred bytes would not
  88. # silently run into the test scaffolding. rt_exec.py asserts
  89. # RT_BASE + blob size <= HDR, so the bound is a checked one.
  90. .set RT_BASE, 0x0190
  91. .set HDR, 0x0800
  92. .set DATA, 0x0810
  93. .set DLEN, 32 # bytes of data area for initmem to clear
  94. .set STORE, 0x0840 # where a read entry's result is stored
  95. .set WSTR, 0x0850 # a string for the wrstr case: length + chars
  96. .set WSTR_EMPTY, 0x0858 # the same, with a length of 0
  97. .set CASE, 0x0860 # the case record
  98. .set D_DUMPLEN, 0x08A0 # the driver's own three words
  99. .set D_DUMPADR, 0x08A2
  100. .set D_CNT, 0x08A4
  101. .set RDSTR_DST, 0x08B0 # destination for the rdstr cases
  102. .set SST_HELLO, 0x08C0 # a short source string for strstore
  103. .set SST_LONG, 0x08D0 # a source longer than a small max
  104. .set SST_EMPTY, 0x08E0 # an empty source string
  105. .set SST_DST, 0x08F0 # destination for the strstore cases
  106. .set SST_CAT, 0x0900 # destination for the strcat cases, with a
  107. # length to append to -- unlike SST_DST,
  108. # which strstore overwrites
  109. .set SST_CAT0, 0x0910 # ...and one that starts empty
  110. .set SST_WORLD, 0x0920 # a source distinct from SST_HELLO
  111. .set R_BLK1, 0x0930
  112. .set R_BLK2, 0x0940
  113. .set R_BLK3, 0x0950
  114. .set R_BLK4, 0x0960
  115. .set R_BLK5, 0x0970
  116. .set R_BLK6, 0x0980
  117. .set R_BLK7, 0x0990
  118. .set R_BLK8, 0x09A0
  119. .set R_BLK9, 0x09B0
  120. .set R_BLK10, 0x09C0
  121. .code16
  122. .text
  123. .globl _start
  124. # The entry JMP -- the same three bytes every image this compiler writes opens
  125. # with. bootcom.s jumps to 0000:0100, which is this instruction. It is
  126. # written as raw bytes rather than `jmp drive' because gas would shorten that to
  127. # a two-byte EB 00 short jump, and the byte it dropped would then be executed as
  128. # the first instruction of the driver.
  129. _start:
  130. .byte 0xE9 # JMP rel16
  131. .word drive - _start - 3
  132. # --------------------------------------------------------------- the driver
  133. drive:
  134. cld
  135. # 1. Poison the two places a check is allowed to look. STORE gets EEEH so
  136. # a read entry that stores nothing is visible as such, and the data
  137. # area gets AAH so initmem has something to clear. The storage below
  138. # is zeroed; the poison is what turns a zero into a RESULT.
  139. movw $STORE + LOAD_BIAS, %si
  140. movw $0xEEEE, %ax
  141. movw %ax, (%si)
  142. movw $DATA + LOAD_BIAS, %si
  143. movw $DLEN, %cx
  144. movb $0xAA, %al
  145. .Lpoison:
  146. movb %al, (%si)
  147. incw %si
  148. loop .Lpoison
  149. # BP is the case-record cursor, and it is the only register that survives a
  150. # call into the runtime, so everything the loop needs is read through it
  151. # or parked in the driver's own words first.
  152. movw $CASE + LOAD_BIAS, %bp
  153. # 2. Open the record: SOH, the case index as two hex digits, STX.
  154. movb $SOH, %al
  155. call putc
  156. movb OFF_CASE(%bp), %al
  157. call hexb
  158. movb $STX, %al
  159. call putc
  160. # 3. Arm the input. INCUR is an INDEX into INBUF, and zeroing it is what
  161. # makes this boot's input start at its first byte. CX is cleared
  162. # first: see the note at the top about the high byte of CX.
  163. xorw %cx, %cx
  164. movb OFF_INLEN(%bp), %cl
  165. movw %cx, INLEN
  166. movw $0, INCUR
  167. movw $CASE + OFF_INBUF + LOAD_BIAS, %si
  168. movw $INBUF, %di
  169. xorw %ax, %ax
  170. repe cmpsb
  171. # 4. The calls. dumplen and the report address are needed after the loop,
  172. # by which time BP has moved, so they are parked now; so is the count,
  173. # because CX is gone by the second iteration.
  174. movw OFF_DUMPLEN(%bp), %ax
  175. movw %ax, D_DUMPLEN + LOAD_BIAS
  176. movw OFF_DUMPADR(%bp), %ax
  177. movw %ax, D_DUMPADR + LOAD_BIAS
  178. movw OFF_NCALLS(%bp), %ax
  179. movw %ax, D_CNT + LOAD_BIAS
  180. addw $SLOT0, %bp # BP now points AT the first call slot
  181. .Lcall:
  182. decw D_CNT + LOAD_BIAS # 1 -> 0 has SF clear, so the body runs once;
  183. jns .Lgo # 0 -> FFFF is what ends the loop
  184. jmp .Lcalldone
  185. .Lgo:
  186. call doslot
  187. addw $SLOT_SZ, %bp
  188. jmp .Lcall
  189. .Lcalldone:
  190. # 5. Close the output half and report the memory. A store that never
  191. # happened is the EEEH left in step 1, which is why it is there.
  192. movb $ETX, %al
  193. call putc
  194. movw D_DUMPLEN + LOAD_BIAS, %cx
  195. jcxz .Lnodump
  196. movw D_DUMPADR + LOAD_BIAS, %si
  197. addw $LOAD_BIAS, %si
  198. .Ldump:
  199. lodsb
  200. call hexb
  201. loop .Ldump
  202. .Lnodump:
  203. movb $EOT, %al
  204. call putc
  205. # 6. Exit. Reaching here IS the assertion that every entry returned: an
  206. # entry that hung produces no EOT, and the harness says so.
  207. movw $0x4C00, %ax # INT 21h AH=4Ch, AL=0
  208. int $0x21
  209. # doslot: call the entry described by the 12 bytes at BP, with the same
  210. # conventions Compiler.IoCall uses -- 16-bit argument(s) on the stack, popped
  211. # by the caller -- except for initmem, which takes the header in AX.
  212. #
  213. # Flags (the byte at +4):
  214. # 1 F_STACK push the word at +2; the caller pops one word.
  215. # 2 F_AX take the word at +2 in AX instead.
  216. # 4 F_INL the argument follows the call, not on the stack.
  217. # 8 F_STACK2 push the word at +6 FIRST and the word at +2 second, so +2
  218. # lands at [BP+4] and +6 at [BP+6]; the caller pops two words.
  219. # strstore wants exactly that: dest at [BP+4], source at [BP+6].
  220. # 16 F_CX load CX from the word at +8 before the call. rdstr and
  221. # strstore both take their maximum length in CX, a register the
  222. # record has no other way to reach, and neither may borrow it
  223. # from whatever the previous case left behind.
  224. #
  225. # The flags are re-read after the call because the entry has just used AX.
  226. doslot:
  227. movb 4(%bp), %al
  228. testb $8, %al
  229. jnz .Ltwostack
  230. testb $1, %al
  231. jz .Lnostack
  232. pushw 2(%bp)
  233. jmp .Lnostack
  234. .Ltwostack:
  235. pushw 6(%bp) # arg B: the source, ends at [BP+6]
  236. pushw 2(%bp) # arg A: the destination, ends at [BP+4]
  237. .Lnostack:
  238. testb $2, %al
  239. jz .Lnoax
  240. movw 2(%bp), %ax
  241. .Lnoax:
  242. testb $16, %al
  243. jz .Lnocx
  244. movw 8(%bp), %cx
  245. .Lnocx:
  246. movw (%bp), %si # image-absolute, as RT_Entry reports it
  247. addw $LOAD_BIAS, %si # so the load bias is added here, not baked in
  248. testb $4, %al
  249. jnz .Linl
  250. call *%si
  251. movb 4(%bp), %al
  252. testb $8, %al
  253. jnz .Lcleantwo
  254. testb $1, %al
  255. jz .Lnoclean
  256. addw $2, %sp # caller-cleaned
  257. .Lnoclean:
  258. ret
  259. .Lcleantwo:
  260. addw $4, %sp
  261. ret
  262. # wrtinl's argument is NOT a stack word: the entry reads a length byte and that
  263. # many characters from its own return address. That is the caller's half of
  264. # the contract, so it is written literally here -- the length byte and the
  265. # characters must be the bytes immediately after the call, which is why this is
  266. # a second copy of the instruction rather than a jump. The compiler's own
  267. # placement of the same three things is what the string-literal fixtures in
  268. # run_com_exec.py check; duplicating a variable one here would be a second
  269. # thing to keep right, and a check that cannot fail is not a check.
  270. .Linl:
  271. call *%si
  272. .byte 5
  273. .byte 'h', 'e', 'l', 'l', 'o'
  274. ret
  275. # putc: AL to the serial port. No status polling -- qemu's 16550 always accepts,
  276. # and a poll that never went ready would hang the harness instead of failing it.
  277. putc:
  278. pushw %ax
  279. movw $0x03f8, %dx
  280. outb %al, %dx
  281. popw %ax
  282. ret
  283. # hexb: AL as two uppercase hex digits, high nibble first.
  284. #
  285. # The 8086 has no `SHR r/m16, imm8' and no shift-by-four, so the high nibble
  286. # comes from four one-bit shifts through CL. Both facts have to be written
  287. # down: gas assembles `shr $4, %ah' without complaint and rejects `shrw $1, %ah'
  288. # outright, so the only thing standing between this file and an image that
  289. # assembles cleanly and dies on a real 8086 is refusing the modern encodings.
  290. hexb:
  291. pushw %ax
  292. pushw %cx
  293. movb %al, %ah
  294. movb $1, %cl
  295. shrw %cl, %ax
  296. shrw %cl, %ax
  297. shrw %cl, %ax
  298. shrw %cl, %ax
  299. popw %cx
  300. call putdig
  301. popw %ax
  302. andb $0x0f, %al
  303. movb %al, %ah
  304. call putdig
  305. ret
  306. # putdig: the nibble in AH as one character, moved into AL so that setting AH for
  307. # INT 21h AH=02h cannot overwrite the digit it is about to print.
  308. putdig:
  309. movb %ah, %al
  310. cmpb $10, %al
  311. jb .Ldigit
  312. addb $0x37, %al # 0Ah + 37h = 'A'
  313. jmp .Lemit
  314. .Ldigit:
  315. addb $0x30, %al # '0'
  316. .Lemit:
  317. movb $2, %ah
  318. int $0x21
  319. ret
  320. # --------------------------------------------------------------- the image
  321. # The runtime blob, from tests/RtProbe.mod built at RT_BASE. The gap is the
  322. # checked bound: rt_exec.py asserts that 0x0180..RT_BASE is still all zeros, so
  323. # a driver that outgrew its space fails the suite instead of overwriting the
  324. # library it is testing.
  325. .org RT_BASE
  326. .incbin "rtblob.bin"
  327. # The program header initmem is handed: +0 flag, +2 code, +4 data base, +6 data
  328. # end. Built HERE, from the same DATA and DLEN the poison loop uses, because
  329. # the compiler's own header layout is a separate claim with its own check and
  330. # this harness is not that check. The two fields initmem READS are memory
  331. # addresses, so they carry the bias; the caller passes AX the header's memory
  332. # address too, which is the one thing rt_exec.py gets right only by saying so
  333. # here and in case_bytes().
  334. .org HDR
  335. HDR:
  336. .word 1 # hdrFlag
  337. .word 0 # hdrCS, unused by initmem
  338. .word DATA + LOAD_BIAS # hdrDS = data base
  339. .word DATA + DLEN + LOAD_BIAS # hdrHeap = data end
  340. .org DATA
  341. .zero DLEN # poisoned to AAH by the driver
  342. .org STORE
  343. .zero 2 # poisoned to EEEH by the driver
  344. # A string for the wrstr case: a length byte and that many characters, which is
  345. # the layout a compiled `string [n]' variable has. It sits above the poisoned
  346. # area so the poison never touches it, and rt_exec.py restates its address - the
  347. # two must agree or the case reads whatever bytes are there and prints them.
  348. .org WSTR
  349. .byte 5
  350. .byte 'h', 'e', 'l', 'l', 'o'
  351. .org WSTR_EMPTY
  352. .byte 0 # a length of 0: an empty string
  353. .org CASE
  354. .incbin "rtcase.bin"
  355. .org D_DUMPLEN
  356. .zero 6 # D_DUMPLEN, D_DUMPADR, D_CNT
  357. # --- strings for the string entries ----------------------------------------
  358. # A compiled `string [n]' variable is a length byte and n characters, and that
  359. # is the layout rdstr writes and strstore reads. Each DESTINATION is
  360. # pre-filled with 0xEE so a byte the entry does NOT write is visibly still
  361. # there -- that is how "the tail past the new length is left untouched" becomes
  362. # a checked claim rather than a comment. The sources are distinct addresses so
  363. # one image can offer a short string, one longer than a small max, and an empty
  364. # one, without any case depending on a previous case's leftovers.
  365. .org RDSTR_DST
  366. .fill 16, 1, 0xEE
  367. .org SST_HELLO
  368. .byte 5, 'h', 'e', 'l', 'l', 'o'
  369. .org SST_LONG
  370. .byte 8, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
  371. .org SST_EMPTY
  372. .byte 0 # a length of 0: an empty string
  373. .org SST_DST
  374. .fill 16, 1, 0xEE
  375. # strcat reads the DESTINATION's length, so its destinations are seeded with a
  376. # real string rather than 0xEE -- and 0xEE would in fact be a 238-character
  377. # length, which is exactly the "string too long" error the entry has to raise.
  378. # SST_CAT holds `hello', SST_CAT0 is empty, and SST_WORLD is a source that is
  379. # not SST_HELLO so an append is visible as an append and not as a coincidence.
  380. .org SST_CAT
  381. .byte 5, 'h', 'e', 'l', 'l', 'o'
  382. .fill 10, 1, 0xEE
  383. .org SST_CAT0
  384. .byte 0 # a length of 0: nothing to append to
  385. .fill 15, 1, 0xEE
  386. .org SST_WORLD
  387. .byte 5, 'w', 'o', 'r', 'l', 'd'
  388. # 12-byte MBF blocks for real_add/real_sub: a at +0, b at +6.
  389. .org R_BLK1
  390. .byte 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00
  391. .org R_BLK2
  392. .byte 0x82, 0x00, 0x00, 0x00, 0x00, 0x40, 0x83, 0x00, 0x00, 0x00, 0x00, 0x20
  393. .org R_BLK3
  394. .byte 0x83, 0x00, 0x00, 0x00, 0x00, 0x20, 0x82, 0x00, 0x00, 0x00, 0x00, 0x40
  395. .org R_BLK4
  396. .byte 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00
  397. .org R_BLK5
  398. .byte 0x8A, 0x00, 0x00, 0x00, 0x00, 0x7A, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00
  399. .org R_BLK6
  400. .byte 0x82, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00
  401. .org R_BLK7
  402. .byte 0x86, 0x00, 0x00, 0x00, 0x00, 0x28, 0x8A, 0x00, 0x00, 0x00, 0x00, 0x7A
  403. .org R_BLK8
  404. .byte 0x8F, 0x00, 0x00, 0x00, 0xFE, 0x7F, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00
  405. .org R_BLK9
  406. .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0x00, 0x00, 0x00, 0x00, 0x20
  407. .org R_BLK10
  408. .byte 0x82, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x82, 0x00, 0x00, 0x00, 0x00, 0x80