bootcom.s 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. # 7000h sits above the largest image that may reach 6F00h and below the
  41. # boot sector at 7C00h, and is simply part of the disk image the harness
  42. # writes, so the addresses here are assembly constants.
  43. #
  44. # The handler owns no scratch inside the loaded region. It used to keep
  45. # the AH=09h cursor in memory at 0702h -- image offset 602h, inside every
  46. # fixture's image, because a .COM is padded out to the data base at
  47. # rtSz+1000h. That was a real limit, and it was hit: string concatenation
  48. # emits enough code that a program's code can reach 602h, and the AH=09h
  49. # that prints a writeln's CRLF then overwrote that program's own `call
  50. # progend'. The cursor lives in SI now, not in memory at all -- lodsb
  51. # advances SI and ser_put leaves SI alone -- so the handler cannot scribble
  52. # on the program, and the code window is the linker's 4 KiB again rather
  53. # than this handler's 602h. See the layout block at the end of this file.
  54. #
  55. # Built and driven by tests/run_com_exec.py. tests/exec/rtdrv.s assembles the
  56. # other half of this machinery -- the driver that calls runtime entries directly
  57. # -- and reuses this file rather than growing a second boot path.
  58. .code16
  59. .text
  60. .globl _start
  61. _start:
  62. cli
  63. xorw %ax, %ax
  64. movw %ax, %ds
  65. movw %ax, %es
  66. movw %ax, %ss
  67. movw $0xfffe, %sp
  68. sti
  69. # IVT entry 21h lives at 0000:0084 (21h * 4): offset word then segment.
  70. # `handler' is a section-relative offset because the section starts at 0;
  71. # the segment this sector was loaded at is 7C00h.
  72. movw $handler, %ax
  73. addw $0x7c00, %ax
  74. movw %ax, 0x84
  75. movw $0, %ax
  76. movw %ax, 0x86
  77. # INT 13h AH=00h: reset the drive controller. A floppy that has just
  78. # been attached needs this before a read will succeed.
  79. movb $0x00, %ah
  80. int $0x13
  81. # Retry the read: qemu's floppy is a file image and a read can fail while
  82. # it settles. Three attempts, then give up loudly.
  83. #
  84. # The read lands at SCRATCH, not at 0100h, and is copied down afterwards.
  85. # That is not ceremony. 0400h-04FFh is the BIOS data area, and SeaBIOS
  86. # keeps live state in it; a transfer whose destination covers that window
  87. # destroys it, and SeaBIOS writes part of it back *after* the DMA, so ten
  88. # bytes of BIOS data end up on top of the image once the transfer is over.
  89. #
  90. # The damage is the worst kind. It is the right length, in the right
  91. # place, and nothing reports an error -- the read sets CF=0, exactly as
  92. # the jnc below expects. It was found by a 19-byte probe that read 0440h
  93. # as its first instruction after the jump and got the BIOS's own bytes
  94. # back, and then confirmed by dumping the whole loaded image: one 10-byte
  95. # run wrong inside an otherwise byte-perfect 512-byte sector, which no
  96. # partial-read or sector-count bug can produce.
  97. #
  98. # 1000:0000 (linear 10000h) is clear of the IVT (0-3FFh), the BDA
  99. # (400-4FFh), SeaBIOS's stack at 700h and the option-ROM window at C000h.
  100. # REP MOVSW is executed code, so the copy is the LAST thing that touches
  101. # 0100h-7100h, and it stops below the boot sector at 7C00h. The copy is
  102. # what the program then runs out of, unchanged.
  103. # The image is read one track at a time. A single INT 13h call cannot
  104. # cross a track reliably, and a per-sector loop has to keep CHS and the
  105. # destination across a BIOS call that may clobber any register. Five
  106. # fixed calls are simpler: track 0 starts at sector 2 (sector 1 is this
  107. # boot sector), every later track starts at sector 1, and the last read
  108. # takes the one sector that is left of the 72.
  109. .set READS, 5
  110. # Read the image one sector at a time. The loop state lives in registers,
  111. # and every register is pushed around INT 13h: the BIOS is allowed to
  112. # clobber all of them, and POP does not touch the carry, so the error flag
  113. # survives the restore. One sector per call also cannot cross a track.
  114. movw $SECTORS, %si # sectors still to read
  115. movw $SCRATCH_SEG, %ax
  116. movw %ax, %es
  117. xorw %bx, %bx # destination offset in ES
  118. xorw %cx, %cx # CH=cylinder 0, CL=sector 0
  119. xorw %dx, %dx # DH=head 0, DL=drive A
  120. movb $2, %cl # sector 2 -- the .COM, 1-based
  121. .Lread:
  122. movw $3, %bp
  123. .Lretry:
  124. movb $0x02, %ah
  125. movb $1, %al
  126. pushw %es
  127. pushw %bp
  128. pushw %si
  129. pushw %bx
  130. pushw %cx
  131. pushw %dx
  132. int $0x13
  133. popw %dx
  134. popw %cx
  135. popw %bx
  136. popw %si
  137. popw %bp
  138. popw %es
  139. jnc .Lreadok
  140. decw %bp
  141. jnz .Lretry
  142. jmp .Lbootfail
  143. .Lreadok:
  144. addw $512, %bx
  145. incb %cl
  146. cmpb $19, %cl
  147. jb .Lreadnext
  148. movb $1, %cl
  149. xorb $1, %dh
  150. jnz .Lreadnext
  151. incb %ch
  152. .Lreadnext:
  153. decw %si
  154. jnz .Lread
  155. jmp .Lcopy
  156. .Lbootfail:
  157. movb $0x7f, %al
  158. movw $0x0501, %dx # isa-debug-exit
  159. outb %al, %dx
  160. cli
  161. hlt
  162. .Lcopy:
  163. movw $SCRATCH_SEG, %ax
  164. movw %ax, %ds
  165. xorw %si, %si # source 1000:0000
  166. xorw %ax, %ax
  167. movw %ax, %es
  168. movw $0x0100, %di # destination 0000:0100
  169. movw $(COPY_BYTES / 2), %cx # bytes / 2
  170. cld
  171. rep movsw
  172. xorw %ax, %ax
  173. movw %ax, %ds # the .COM expects DS=0
  174. .byte 0xEA, 0x00, 0x01, 0x00, 0x00 # jmp 0000:0100
  175. # ---------------------------------------------------------------- INT 21h
  176. # Called with the program's registers. DS is the caller's data segment
  177. # (0000h here) and is preserved, so AH=09h can reach DS:DX the way DOS does.
  178. # Every path ends at .Ldone, which restores everything and IRETs -- except
  179. # AH=4Ch, which does not return at all.
  180. handler:
  181. pushw %ax
  182. pushw %bx
  183. pushw %cx
  184. pushw %dx
  185. pushw %si
  186. pushw %di
  187. pushw %ds
  188. pushw %es
  189. # Direction flag, before anything reads it. Both lodsb's below walk
  190. # FORWARD, and DF belongs to the interrupted program, not to the
  191. # handler: a handler that assumes the caller's DF is clear is correct
  192. # only for as long as no caller ever sets it. It has never fired -
  193. # nothing in Runtime.mod uses a string instruction, so DF is whatever
  194. # the BIOS left, and that is 0 - which is exactly why it is worth
  195. # writing down. DF is not restored, because nothing downstream reads
  196. # it and restoring it would mean saving EFLAGS around the whole handler.
  197. cld
  198. cmpb $0x02, %ah
  199. je .Lh02
  200. cmpb $0x09, %ah
  201. je .Lh09
  202. cmpb $0x08, %ah
  203. je .Lh08
  204. cmpb $0x0b, %ah
  205. je .Lh0b
  206. cmpb $0x4c, %ah
  207. je .Lh4c
  208. stc # unknown function
  209. jmp .Ldone
  210. .Lh02: # display character in AL
  211. call ser_put
  212. clc
  213. jmp .Ldone
  214. .Lh09: # display the $-terminated string at DS:DX
  215. movw %dx, %si # SI is the cursor. lodsb walks it forward,
  216. .Lh09next: # and ser_put leaves SI alone -- it clobbers
  217. lodsb # only DX, which is why the pointer cannot
  218. cmpb $0x24, %al # simply stay in DX across the call.
  219. je .Lh09done
  220. call ser_put
  221. jmp .Lh09next
  222. .Lh09done:
  223. clc
  224. jmp .Ldone
  225. .Lh08: # read a character, no echo, 1Ah at EOF
  226. # NOTE: this is the one function whose RESULT is in AL, so it must return
  227. # through .Ldone8 and not .Ldone - see there.
  228. movw INLEN, %ax # inlen
  229. movw INCUR, %bx # input cursor
  230. # EOF is when the cursor has REACHED the length, i.e. INCUR >= INLEN.
  231. # `cmpw %bx, %ax / jbe' tests AX <= BX, that is INLEN <= INCUR, which is
  232. # true on the FIRST character: it returned 1Ah immediately, so readln saw
  233. # an empty input and then looped for the line terminator that never came.
  234. # t29_readln hung with no output at all. (This was a bug in the harness,
  235. # not in the compiler - but a harness that feeds the program nothing can
  236. # never tell you whether the program handles input, so it is a bug that
  237. # hides bugs.)
  238. cmpw %ax, %bx # INCUR vs INLEN
  239. jae .Lh08eof
  240. # INCUR is an index INTO the buffer, not an address: the bytes live at
  241. # INBUF, and SI = INCUR alone reads the interrupt vector table at 0000:0000
  242. # - so AH=08h returned IVT[0] (a low timer vector byte) as the first
  243. # character of input. INCUR is 0 on the first call, which is the one
  244. # address in segment 0 that is guaranteed to be wrong.
  245. movw %bx, %si
  246. addw $INBUF, %si
  247. lodsb
  248. incw INCUR
  249. clc
  250. jmp .Ldone8 # NOT .Ldone: AL is the result here
  251. .Lh08eof:
  252. movb $0x1a, %al
  253. clc
  254. jmp .Ldone8
  255. .Lh0b: # input status: AL=FFh if a character waits
  256. movw INLEN, %ax
  257. movw INCUR, %bx
  258. cmpw %ax, %bx # INCUR vs INLEN: >= is end of input
  259. jae .Lh0b_none
  260. movb $0xff, %al
  261. clc
  262. jmp .Ldone8 # AL is the result, as with AH=08h
  263. .Lh0b_none:
  264. xorw %ax, %ax # AL = 0: nothing waiting
  265. clc
  266. jmp .Ldone8
  267. .Lh4c: # terminate: exit with AL as the code
  268. movw $0x0501, %dx # isa-debug-exit
  269. outb %al, %dx
  270. cli
  271. hlt
  272. jmp .Lh4c
  273. .Ldone:
  274. popw %es
  275. popw %ds
  276. popw %di
  277. popw %si
  278. popw %dx
  279. popw %cx
  280. popw %bx
  281. popw %ax
  282. iret
  283. # The same, but for the one function that RETURNS something in AL. DOS
  284. # AH=08h hands the character back in AL, so restoring AX on the way out
  285. # throws the answer away and the caller reads whatever AX held on entry.
  286. #
  287. # This was silent in a way that is worth recording: the shim's read path was
  288. # structurally correct - it found the buffer, advanced the cursor, cleared
  289. # the carry - and the character was plainly in AL, one instruction before the
  290. # return. The discard happened in the epilogue, which every OTHER function
  291. # needs. So t29_readln did not see a wrong byte; it saw the *uninitialised*
  292. # AX the runtime had at the call, which is the first byte of a pointer it was
  293. # about to overwrite with the parsed value. readln compared that against 0Dh,
  294. # 0Ah and 1Ah, rejected it, and looped forever - a hang with no output, from
  295. # a read that demonstrably worked.
  296. #
  297. # AX is therefore popped only on the paths where it is not a result, and
  298. # AH=02h/09h (which also leave AL alone in DOS) keep using .Ldone.
  299. .Ldone8:
  300. popw %es
  301. popw %ds
  302. popw %di
  303. popw %si
  304. popw %dx
  305. popw %cx
  306. popw %bx
  307. addw $2, %sp # drop the saved AX, keep AL
  308. iret
  309. # ser_put: send AL to the serial port, leaving AL and DX alone.
  310. # No line-status polling: qemu's 16550 always accepts a byte, and a poll
  311. # that never goes ready would hang the harness rather than fail it.
  312. ser_put:
  313. pushw %ax
  314. movw $0x03f8, %dx
  315. outb %al, %dx
  316. popw %ax
  317. ret
  318. # --------------------------------------------------------------- layout
  319. # The input descriptor lives at 7000h. The image may reach 6F00h, the copy
  320. # ends at 7100h, and the boot sector at 7C00h is never touched; the read covers
  321. # enough sectors to bring the descriptor down with the image. So it is simply
  322. # part of the disk image the harness writes, not something it has to
  323. # patch into this boot sector afterwards -- which means the addresses here are
  324. # assembly constants and the harness only has to know where they land in the
  325. # file. See run_com_exec.py, FLAT_OFF.
  326. #
  327. # SCRATCH is where the sector read parks the bytes before the copy moves them
  328. # to 0100h. Nothing may ever be placed there: the region is not reserved by
  329. # this file, it is merely unused, and a future change that put a table at
  330. # 10000h would be overwritten by the read and would not notice.
  331. .set SECTORS, 72 # 36,864 bytes read; only COPY_BYTES is moved down
  332. .set COPY_BYTES, 0x7000 # up to the input, below the boot sector at 7C00h
  333. .set SCRATCH_SEG, 0x1000 # read buffer at 1000:0000, outside the 64K
  334. .set INLEN, 0x7000 # word: number of input bytes
  335. .set INCUR, 0x7002 # word: cursor, starts at INBUF
  336. .set INBUF, 0x7004 # the bytes themselves
  337. .set INMAX, 0x00fc # 7100h - 7004h, the most that fits
  338. .org 510
  339. .byte 0x55, 0xAA