bootcom.s 11 KB

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