bootcom.s 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 $0x3d, %ah
  207. je .Lh3d
  208. cmpb $0x4c, %ah
  209. je .Lh4c
  210. stc # unknown function
  211. jmp .Ldone
  212. .Lh02: # display character in AL
  213. call ser_put
  214. clc
  215. jmp .Ldone
  216. .Lh09: # display the $-terminated string at DS:DX
  217. movw %dx, %si # SI is the cursor. lodsb walks it forward,
  218. .Lh09next: # and ser_put leaves SI alone -- it clobbers
  219. lodsb # only DX, which is why the pointer cannot
  220. cmpb $0x24, %al # simply stay in DX across the call.
  221. je .Lh09done
  222. call ser_put
  223. jmp .Lh09next
  224. .Lh09done:
  225. clc
  226. jmp .Ldone
  227. .Lh08: # read a character, no echo, 1Ah at EOF
  228. # NOTE: this is the one function whose RESULT is in AL, so it must return
  229. # through .Ldone8 and not .Ldone - see there.
  230. movw INLEN, %ax # inlen
  231. movw INCUR, %bx # input cursor
  232. # EOF is when the cursor has REACHED the length, i.e. INCUR >= INLEN.
  233. # `cmpw %bx, %ax / jbe' tests AX <= BX, that is INLEN <= INCUR, which is
  234. # true on the FIRST character: it returned 1Ah immediately, so readln saw
  235. # an empty input and then looped for the line terminator that never came.
  236. # t29_readln hung with no output at all. (This was a bug in the harness,
  237. # not in the compiler - but a harness that feeds the program nothing can
  238. # never tell you whether the program handles input, so it is a bug that
  239. # hides bugs.)
  240. cmpw %ax, %bx # INCUR vs INLEN
  241. jae .Lh08eof
  242. # INCUR is an index INTO the buffer, not an address: the bytes live at
  243. # INBUF, and SI = INCUR alone reads the interrupt vector table at 0000:0000
  244. # - so AH=08h returned IVT[0] (a low timer vector byte) as the first
  245. # character of input. INCUR is 0 on the first call, which is the one
  246. # address in segment 0 that is guaranteed to be wrong.
  247. movw %bx, %si
  248. addw $INBUF, %si
  249. lodsb
  250. incw INCUR
  251. clc
  252. jmp .Ldone8 # NOT .Ldone: AL is the result here
  253. .Lh08eof:
  254. movb $0x1a, %al
  255. clc
  256. jmp .Ldone8
  257. .Lh0b: # input status: AL=FFh if a character waits
  258. movw INLEN, %ax
  259. movw INCUR, %bx
  260. cmpw %ax, %bx # INCUR vs INLEN: >= is end of input
  261. jae .Lh0b_none
  262. movb $0xff, %al
  263. clc
  264. jmp .Ldone8 # AL is the result, as with AH=08h
  265. .Lh0b_none:
  266. xorw %ax, %ax # AL = 0: nothing waiting
  267. clc
  268. jmp .Ldone8
  269. .Lh3d: # open file: no filesystem in this harness
  270. # The runtime uses real DOS AH=3Dh. A boot floppy has no DOS and no
  271. # filesystem driver, so answer the one question the file-operation
  272. # fixture asks - "the file is absent" - the way DOS does: CF=1,
  273. # AX=2 (File not found). The result must survive the epilogue, and
  274. # CF must be visible to the guest, so this uses .LdoneFile.
  275. movw $2, %ax
  276. stc
  277. jmp .LdoneFile
  278. .Lh4c: # terminate: exit with AL as the code
  279. movw $0x0501, %dx # isa-debug-exit
  280. outb %al, %dx
  281. cli
  282. hlt
  283. jmp .Lh4c
  284. .Ldone:
  285. popw %es
  286. popw %ds
  287. popw %di
  288. popw %si
  289. popw %dx
  290. popw %cx
  291. popw %bx
  292. popw %ax
  293. iret
  294. # The same, but for the one function that RETURNS something in AL. DOS
  295. # AH=08h hands the character back in AL, so restoring AX on the way out
  296. # throws the answer away and the caller reads whatever AX held on entry.
  297. #
  298. # This was silent in a way that is worth recording: the shim's read path was
  299. # structurally correct - it found the buffer, advanced the cursor, cleared
  300. # the carry - and the character was plainly in AL, one instruction before the
  301. # return. The discard happened in the epilogue, which every OTHER function
  302. # needs. So t29_readln did not see a wrong byte; it saw the *uninitialised*
  303. # AX the runtime had at the call, which is the first byte of a pointer it was
  304. # about to overwrite with the parsed value. readln compared that against 0Dh,
  305. # 0Ah and 1Ah, rejected it, and looped forever - a hang with no output, from
  306. # a read that demonstrably worked.
  307. #
  308. # AX is therefore popped only on the paths where it is not a result, and
  309. # AH=02h/09h (which also leave AL alone in DOS) keep using .Ldone.
  310. .LdoneFile:
  311. # AX is the result and CF is the DOS error flag. POP does not change
  312. # CF, so restore the saved registers first, then copy CF into the saved
  313. # FLAGS word before dropping the saved AX and IRETing. The 16-bit ADD
  314. # used to discard AX would clobber CF, so set FLAGS before it.
  315. popw %es
  316. popw %ds
  317. popw %di
  318. popw %si
  319. popw %dx
  320. popw %cx
  321. popw %bx
  322. # 16-bit addressing has no [SP] form. Borrow BX as the frame pointer
  323. # for two instructions; POP does not affect CF, so the branch still sees
  324. # the handler's result. After the push, SP points at the saved BX, so
  325. # saved AX is +2 and saved FLAGS are +8.
  326. pushw %bx
  327. movw %sp, %bx
  328. jnc .Ldf_ok
  329. orw $1, 8(%bx) # saved FLAGS: set CF
  330. jmp .Ldf_drop
  331. .Ldf_ok:
  332. andw $0xfffe, 8(%bx) # saved FLAGS: clear CF
  333. .Ldf_drop:
  334. popw %bx
  335. addw $2, %sp # drop the saved AX, keep the result
  336. iret
  337. .Ldone8:
  338. popw %es
  339. popw %ds
  340. popw %di
  341. popw %si
  342. popw %dx
  343. popw %cx
  344. popw %bx
  345. addw $2, %sp # drop the saved AX, keep AL
  346. iret
  347. # ser_put: send AL to the serial port, leaving AL and DX alone.
  348. # No line-status polling: qemu's 16550 always accepts a byte, and a poll
  349. # that never goes ready would hang the harness rather than fail it.
  350. ser_put:
  351. pushw %ax
  352. movw $0x03f8, %dx
  353. outb %al, %dx
  354. popw %ax
  355. ret
  356. # --------------------------------------------------------------- layout
  357. # The input descriptor lives at 7000h. The image may reach 6F00h, the copy
  358. # ends at 7100h, and the boot sector at 7C00h is never touched; the read covers
  359. # enough sectors to bring the descriptor down with the image. So it is simply
  360. # part of the disk image the harness writes, not something it has to
  361. # patch into this boot sector afterwards -- which means the addresses here are
  362. # assembly constants and the harness only has to know where they land in the
  363. # file. See run_com_exec.py, FLAT_OFF.
  364. #
  365. # SCRATCH is where the sector read parks the bytes before the copy moves them
  366. # to 0100h. Nothing may ever be placed there: the region is not reserved by
  367. # this file, it is merely unused, and a future change that put a table at
  368. # 10000h would be overwritten by the read and would not notice.
  369. .set SECTORS, 72 # 36,864 bytes read; only COPY_BYTES is moved down
  370. .set COPY_BYTES, 0x7000 # up to the input, below the boot sector at 7C00h
  371. .set SCRATCH_SEG, 0x1000 # read buffer at 1000:0000, outside the 64K
  372. .set INLEN, 0x7000 # word: number of input bytes
  373. .set INCUR, 0x7002 # word: cursor, starts at INBUF
  374. .set INBUF, 0x7004 # the bytes themselves
  375. .set INMAX, 0x00fc # 7100h - 7004h, the most that fits
  376. .org 510
  377. .byte 0x55, 0xAA