bootcom.s 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # bootcom.s -- load a DOS .COM from the boot floppy and run it, with INT 21h
  2. # wired to the serial port instead of a screen.
  3. #
  4. # This is the piece that lets a .COM produced by this compiler actually run.
  5. # It is deliberately not DOS: it is a 512-byte boot sector that does the four
  6. # things a DOS .COM loader does, and sends everything the program prints out
  7. # the serial port so a test harness can read it.
  8. #
  9. # 1. install an INT 21h vector pointing at a handler below
  10. # 2. INT 13h AH=02h: read the .COM off drive A: to 0000:0100
  11. # 3. SS:SP = 0000:FFFE, the segment top -- where DOS puts a .COM's stack
  12. # 4. JMP 0000:0100
  13. #
  14. # The INT 21h handler implements the four functions this runtime actually
  15. # calls, and nothing else:
  16. #
  17. # AH=02h display character in AL
  18. # AH=09h display the $-terminated string at DS:DX
  19. # AH=08h read a character without echo; 1Ah at end of input
  20. # AH=0Bh input status: AL=FFh if a character waits, 0 if not
  21. # AH=4Ch terminate
  22. #
  23. # Those five are the complete set. See Runtime.mod: every Int21 in it is one
  24. # of them (the "MovAh (0)" in EmitRdInt is not an INT 21h call, it is the
  25. # MOV AH,0 that loads a digit into AL before an ADD -- an easy thing to
  26. # misread as a fourth function).
  27. #
  28. # Two properties are load-bearing:
  29. #
  30. # * The handler NEVER writes to the serial port as a marker. The program's
  31. # output is arbitrary bytes, so any sentinel could collide with real
  32. # output. Termination travels out of band instead, through the
  33. # isa-debug-exit device at port 0501h: the program exits with code 0 and
  34. # qemu exits (value<<1)|1 = 1. A boot failure exits with value 7Fh, i.e.
  35. # qemu exit code 255, which is unambiguous. Every byte on the serial port
  36. # is therefore program output, with nothing to strip.
  37. #
  38. # * The program is loaded at 0000:0100, so segment 0 holds the IVT at
  39. # 0000:0000-00FF and the program from 0100 up. The input descriptor at
  40. # 2000h sits past the end of any fixture and is simply part of the disk
  41. # image the harness writes, so the addresses here are assembly constants.
  42. #
  43. # The handler owns no scratch inside the loaded region. It used to keep
  44. # the AH=09h cursor in memory at 0702h -- image offset 602h, inside every
  45. # fixture's image, because a .COM is padded out to the data base at
  46. # rtSz+1000h. That was a real limit, and it was hit: string concatenation
  47. # emits enough code that a program's code can reach 602h, and the AH=09h
  48. # that prints a writeln's CRLF then overwrote that program's own `call
  49. # progend'. The cursor lives in SI now, not in memory at all -- lodsb
  50. # advances SI and ser_put leaves SI alone -- so the handler cannot scribble
  51. # on the program, and the code window is the linker's 4 KiB again rather
  52. # than this handler's 602h. See the layout block at the end of this file.
  53. #
  54. # Built and driven by tests/run_com_exec.py. tests/exec/rtdrv.s assembles the
  55. # other half of this machinery -- the driver that calls runtime entries directly
  56. # -- and reuses this file rather than growing a second boot path.
  57. .code16
  58. .text
  59. .globl _start
  60. _start:
  61. cli
  62. xorw %ax, %ax
  63. movw %ax, %ds
  64. movw %ax, %es
  65. movw %ax, %ss
  66. movw $0xfffe, %sp
  67. sti
  68. # IVT entry 21h lives at 0000:0084 (21h * 4): offset word then segment.
  69. # `handler' is a section-relative offset because the section starts at 0;
  70. # the segment this sector was loaded at is 7C00h.
  71. movw $handler, %ax
  72. addw $0x7c00, %ax
  73. movw %ax, 0x84
  74. movw $0, %ax
  75. movw %ax, 0x86
  76. # INT 13h AH=00h: reset the drive controller. A floppy that has just
  77. # been attached needs this before a read will succeed.
  78. movb $0x00, %ah
  79. int $0x13
  80. # Retry the read: qemu's floppy is a file image and a read can fail while
  81. # it settles. Three attempts, then give up loudly.
  82. #
  83. # The read lands at SCRATCH, not at 0100h, and is copied down afterwards.
  84. # That is not ceremony. 0400h-04FFh is the BIOS data area, and SeaBIOS
  85. # keeps live state in it; a transfer whose destination covers that window
  86. # destroys it, and SeaBIOS writes part of it back *after* the DMA, so ten
  87. # bytes of BIOS data end up on top of the image once the transfer is over.
  88. #
  89. # The damage is the worst kind. It is the right length, in the right
  90. # place, and nothing reports an error -- the read sets CF=0, exactly as
  91. # the jnc below expects. It was found by a 19-byte probe that read 0440h
  92. # as its first instruction after the jump and got the BIOS's own bytes
  93. # back, and then confirmed by dumping the whole loaded image: one 10-byte
  94. # run wrong inside an otherwise byte-perfect 512-byte sector, which no
  95. # partial-read or sector-count bug can produce.
  96. #
  97. # 8000h is clear of the IVT (0-3FFh), the BDA (400-4FFh), SeaBIOS's stack
  98. # at 700h and the option-ROM window at C000h. REP MOVSW is executed code,
  99. # so the copy is the LAST thing that touches 0100h-20FFh and no BIOS call
  100. # follows it. The copy is what the program then runs out of, unchanged.
  101. movw $3, %cx
  102. .Lretry:
  103. movw $SCRATCH, %bx # ES:BX = 0000:8000
  104. movb $0x02, %ah # read sectors
  105. movb $16, %al # 16 * 512 = 8192 bytes, far more than any fixture
  106. movb $0x00, %ch # cylinder 0
  107. movb $0x02, %cl # sector 2 -- the .COM, 1-based
  108. movb $0x00, %dh # head 0
  109. movb $0x00, %dl # drive A
  110. int $0x13
  111. jnc .Lcopy
  112. decw %cx
  113. jnz .Lretry
  114. .Lbootfail:
  115. movb $0x7f, %al
  116. movw $0x0501, %dx # isa-debug-exit
  117. outb %al, %dx
  118. cli
  119. hlt
  120. .Lcopy:
  121. movw $0x0100, %di
  122. movw $SCRATCH, %si
  123. movw $4096, %cx # 8192 bytes = 16 sectors
  124. cld
  125. rep movsw
  126. .byte 0xEA, 0x00, 0x01, 0x00, 0x00 # jmp 0000:0100
  127. # ---------------------------------------------------------------- INT 21h
  128. # Called with the program's registers. DS is the caller's data segment
  129. # (0000h here) and is preserved, so AH=09h can reach DS:DX the way DOS does.
  130. # Every path ends at .Ldone, which restores everything and IRETs -- except
  131. # AH=4Ch, which does not return at all.
  132. handler:
  133. pushw %ax
  134. pushw %bx
  135. pushw %cx
  136. pushw %dx
  137. pushw %si
  138. pushw %di
  139. pushw %ds
  140. pushw %es
  141. # Direction flag, before anything reads it. Both lodsb's below walk
  142. # FORWARD, and DF belongs to the interrupted program, not to the
  143. # handler: a handler that assumes the caller's DF is clear is correct
  144. # only for as long as no caller ever sets it. It has never fired -
  145. # nothing in Runtime.mod uses a string instruction, so DF is whatever
  146. # the BIOS left, and that is 0 - which is exactly why it is worth
  147. # writing down. DF is not restored, because nothing downstream reads
  148. # it and restoring it would mean saving EFLAGS around the whole handler.
  149. cld
  150. cmpb $0x02, %ah
  151. je .Lh02
  152. cmpb $0x09, %ah
  153. je .Lh09
  154. cmpb $0x08, %ah
  155. je .Lh08
  156. cmpb $0x0b, %ah
  157. je .Lh0b
  158. cmpb $0x4c, %ah
  159. je .Lh4c
  160. stc # unknown function
  161. jmp .Ldone
  162. .Lh02: # display character in AL
  163. call ser_put
  164. clc
  165. jmp .Ldone
  166. .Lh09: # display the $-terminated string at DS:DX
  167. movw %dx, %si # SI is the cursor. lodsb walks it forward,
  168. .Lh09next: # and ser_put leaves SI alone -- it clobbers
  169. lodsb # only DX, which is why the pointer cannot
  170. cmpb $0x24, %al # simply stay in DX across the call.
  171. je .Lh09done
  172. call ser_put
  173. jmp .Lh09next
  174. .Lh09done:
  175. clc
  176. jmp .Ldone
  177. .Lh08: # read a character, no echo, 1Ah at EOF
  178. # NOTE: this is the one function whose RESULT is in AL, so it must return
  179. # through .Ldone8 and not .Ldone - see there.
  180. movw INLEN, %ax # inlen
  181. movw INCUR, %bx # input cursor
  182. # EOF is when the cursor has REACHED the length, i.e. INCUR >= INLEN.
  183. # `cmpw %bx, %ax / jbe' tests AX <= BX, that is INLEN <= INCUR, which is
  184. # true on the FIRST character: it returned 1Ah immediately, so readln saw
  185. # an empty input and then looped for the line terminator that never came.
  186. # t29_readln hung with no output at all. (This was a bug in the harness,
  187. # not in the compiler - but a harness that feeds the program nothing can
  188. # never tell you whether the program handles input, so it is a bug that
  189. # hides bugs.)
  190. cmpw %ax, %bx # INCUR vs INLEN
  191. jae .Lh08eof
  192. # INCUR is an index INTO the buffer, not an address: the bytes live at
  193. # INBUF, and SI = INCUR alone reads the interrupt vector table at 0000:0000
  194. # - so AH=08h returned IVT[0] (a low timer vector byte) as the first
  195. # character of input. INCUR is 0 on the first call, which is the one
  196. # address in segment 0 that is guaranteed to be wrong.
  197. movw %bx, %si
  198. addw $INBUF, %si
  199. lodsb
  200. incw INCUR
  201. clc
  202. jmp .Ldone8 # NOT .Ldone: AL is the result here
  203. .Lh08eof:
  204. movb $0x1a, %al
  205. clc
  206. jmp .Ldone8
  207. .Lh0b: # input status: AL=FFh if a character waits
  208. movw INLEN, %ax
  209. movw INCUR, %bx
  210. cmpw %ax, %bx # INCUR vs INLEN: >= is end of input
  211. jae .Lh0b_none
  212. movb $0xff, %al
  213. clc
  214. jmp .Ldone8 # AL is the result, as with AH=08h
  215. .Lh0b_none:
  216. xorw %ax, %ax # AL = 0: nothing waiting
  217. clc
  218. jmp .Ldone8
  219. .Lh4c: # terminate: exit with AL as the code
  220. movw $0x0501, %dx # isa-debug-exit
  221. outb %al, %dx
  222. cli
  223. hlt
  224. jmp .Lh4c
  225. .Ldone:
  226. popw %es
  227. popw %ds
  228. popw %di
  229. popw %si
  230. popw %dx
  231. popw %cx
  232. popw %bx
  233. popw %ax
  234. iret
  235. # The same, but for the one function that RETURNS something in AL. DOS
  236. # AH=08h hands the character back in AL, so restoring AX on the way out
  237. # throws the answer away and the caller reads whatever AX held on entry.
  238. #
  239. # This was silent in a way that is worth recording: the shim's read path was
  240. # structurally correct - it found the buffer, advanced the cursor, cleared
  241. # the carry - and the character was plainly in AL, one instruction before the
  242. # return. The discard happened in the epilogue, which every OTHER function
  243. # needs. So t29_readln did not see a wrong byte; it saw the *uninitialised*
  244. # AX the runtime had at the call, which is the first byte of a pointer it was
  245. # about to overwrite with the parsed value. readln compared that against 0Dh,
  246. # 0Ah and 1Ah, rejected it, and looped forever - a hang with no output, from
  247. # a read that demonstrably worked.
  248. #
  249. # AX is therefore popped only on the paths where it is not a result, and
  250. # AH=02h/09h (which also leave AL alone in DOS) keep using .Ldone.
  251. .Ldone8:
  252. popw %es
  253. popw %ds
  254. popw %di
  255. popw %si
  256. popw %dx
  257. popw %cx
  258. popw %bx
  259. addw $2, %sp # drop the saved AX, keep AL
  260. iret
  261. # ser_put: send AL to the serial port, leaving AL and DX alone.
  262. # No line-status polling: qemu's 16550 always accepts a byte, and a poll
  263. # that never goes ready would hang the harness rather than fail it.
  264. ser_put:
  265. pushw %ax
  266. movw $0x03f8, %dx
  267. outb %al, %dx
  268. popw %ax
  269. ret
  270. # --------------------------------------------------------------- layout
  271. # The input descriptor lives at 2000h, which the read + copy above covers
  272. # (0100h + 2000h = 2100h) and which is well past the end of any fixture (the
  273. # largest is 4493 bytes, so the image stops at 1285h). So the descriptor is
  274. # simply part of the disk image the harness writes, not something it has to
  275. # patch into this boot sector afterwards -- which means the addresses here are
  276. # assembly constants and the harness only has to know where they land in the
  277. # file. See run_com_exec.py, FLAT_OFF.
  278. #
  279. # SCRATCH is where the sector read parks the bytes before the copy moves them
  280. # to 0100h. Nothing may ever be placed there: the region is not reserved by
  281. # this file, it is merely unused, and a future change that put a table at
  282. # 8000h would be overwritten by the read and would not notice.
  283. .set SCRATCH, 0x8000 # see the note above: not 0100h
  284. .set INLEN, 0x2000 # word: number of input bytes
  285. .set INCUR, 0x2002 # word: cursor, starts at INBUF
  286. .set INBUF, 0x2004 # the bytes themselves
  287. .set INMAX, 0x00fc # 2100h - 2004h, the most that fits
  288. .org 510
  289. .byte 0x55, 0xAA