2 # Copyright (c) 1998 Robert Nordier
5 # Redistribution and use in source and binary forms are freely
6 # permitted provided that the above copyright notice and this
7 # paragraph and the following disclaimer are duplicated in all
10 # This software is provided "AS IS" and without any express or
11 # implied warranties, including, without limitation, the implied
12 # warranties of merchantability and fitness for a particular
16 # $FreeBSD: src/sys/boot/pc98/btx/btx/btx.s,v 1.6.2.5 2001/12/27 16:53:08 nyan Exp $
17 # $DragonFly: src/sys/boot/pc98/btx/btx/Attic/btx.s,v 1.2 2003/06/17 04:28:18 dillon Exp $
22 .set MEM_BTX,0x1000 # Start of BTX memory
23 .set MEM_ESP0,0x1800 # Supervisor stack
24 .set MEM_BUF,0x1800 # Scratch buffer
25 .set MEM_ESP1,0x1e00 # Link stack
26 .set MEM_IDT,0x1e00 # IDT
27 .set MEM_TSS,0x1f98 # TSS
28 .set MEM_MAP,0x2000 # I/O bit map
29 .set MEM_DIR,0x4000 # Page directory
30 .set MEM_TBL,0x5000 # Page tables
31 .set MEM_ORG,0x9000 # BTX code
32 .set MEM_USR,0xa000 # Start of user memory
36 .set PAG_SIZ,0x1000 # Page size
37 .set PAG_CNT,0x1000 # Pages to map
41 .set SEL_SCODE,0x8 # Supervisor code
42 .set SEL_SDATA,0x10 # Supervisor data
43 .set SEL_RCODE,0x18 # Real mode code
44 .set SEL_RDATA,0x20 # Real mode data
45 .set SEL_UCODE,0x28|3 # User code
46 .set SEL_UDATA,0x30|3 # User data
47 .set SEL_TSS,0x38 # TSS
49 # Task state segment fields.
51 .set TSS_ESP0,0x4 # PL 0 ESP
52 .set TSS_SS0,0x8 # PL 0 SS
53 .set TSS_ESP1,0xc # PL 1 ESP
54 .set TSS_MAP,0x66 # I/O bit map base
58 .set SYS_EXIT,0x0 # Exit
59 .set SYS_EXEC,0x1 # Exec
63 .set V86_FLG,0x208eff # V86 flag mask
64 .set V86_STK,0x400 # V86 stack allowance
66 # Dump format control bytes.
68 .set DMP_X16,0x1 # Word
69 .set DMP_X32,0x2 # Long
70 .set DMP_MEM,0x4 # Memory
71 .set DMP_EOL,0x8 # End of line
73 # Screen defaults and assumptions.
76 .set SCR_MAT,0xe1 # Mode/attribute
78 .set SCR_MAT,0x7 # Mode/attribute
80 .set SCR_COL,0x50 # Columns per row
81 .set SCR_ROW,0x19 # Rows per screen
83 # BIOS Data Area locations.
86 .set BDA_MEM,0x501 # Free memory
87 .set BDA_KEYFLAGS,0x53a # Keyboard shift-state flags
88 .set BDA_POS,0x53e # Cursor position
90 .set BDA_MEM,0x413 # Free memory
91 .set BDA_KEYFLAGS,0x417 # Keyboard shift-state flags
92 .set BDA_SCR,0x449 # Video mode
93 .set BDA_POS,0x450 # Cursor position
94 .set BDA_BOOT,0x472 # Boot howto flag
97 # Derivations, for brevity.
99 .set _ESP0H,MEM_ESP0>>0x8 # Byte 1 of ESP0
100 .set _ESP1H,MEM_ESP1>>0x8 # Byte 1 of ESP1
101 .set _TSSIO,MEM_MAP-MEM_TSS # TSS I/O base
102 .set _TSSLM,MEM_DIR-MEM_TSS-1 # TSS limit
103 .set _IDTLM,MEM_TSS-MEM_IDT-1 # IDT limit
109 start: # Start of code
113 btx_hdr: .byte 0xeb # Machine ID
114 .byte 0xe # Header size
116 .byte 0x1 # Major version
117 .byte 0x1 # Minor version
118 .byte BTX_FLAGS # Flags
119 .word PAG_CNT-MEM_ORG>>0xc # Paging control
120 .word break-start # Text size
121 .long 0x0 # Entry address
123 # Initialization routine.
125 init: cli # Disable interrupts
126 xor %ax,%ax # Zero/segment
128 mov $MEM_ESP0,%sp # stack
129 mov %ax,%es # Address
136 mov $MEM_IDT,%di # Memory to initialize
137 mov $(MEM_ORG-MEM_IDT)/2,%cx # Words to zero
145 mov $idtctl,%si # Control string
146 init.1: lodsb # Get entry
148 xchg %ax,%cx # as word
149 jcxz init.4 # If done
151 xchg %ax,%dx # P:DPL:type
154 lodsw # Get handler offset
155 mov $SEL_SCODE,%dh # Segment selector
156 init.2: shr %bx # Handle this int?
158 mov %ax,(%di) # Set handler offset
159 mov %dh,0x2(%di) # and selector
160 mov %dl,0x5(%di) # Set P:DPL:type
161 add $0x4,%ax # Next handler
162 init.3: lea 0x8(%di),%di # Next entry
163 loop init.2 # Till set done
164 jmp init.1 # Continue
168 init.4: movb $_ESP0H,TSS_ESP0+1(%di) # Set ESP0
169 movb $SEL_SDATA,TSS_SS0(%di) # Set SS0
170 movb $_ESP1H,TSS_ESP1+1(%di) # Set ESP1
171 movb $_TSSIO,TSS_MAP(%di) # Set I/O bit map base
174 # Create page directory.
177 mov $PAG_SIZ>>0x8,%dh # size
179 mov $MEM_DIR,%di # Page directory
180 mov $PAG_CNT>>0xa,%cl # Entries
181 mov $MEM_TBL|0x7,%ax # First entry
182 init.5: stosl # Write entry
183 add %dx,%ax # To next
184 loop init.5 # Till done
186 # Create page tables.
188 mov $MEM_TBL,%di # Page table
189 mov $PAG_CNT>>0x8,%ch # Entries
190 xor %ax,%ax # Start address
191 init.6: mov $0x7,%al # Set U:W:P flags
192 cmp btx_hdr+0x8,%cx # Standard user page?
194 cmp $PAG_CNT-MEM_BTX>>0xc,%cx # BTX memory?
195 jae init.7 # No or first page
196 and $~0x2,%al # Clear W flag
197 cmp $PAG_CNT-MEM_USR>>0xc,%cx # User page zero?
199 testb $0x80,btx_hdr+0x7 # Unmap it?
201 and $~0x1,%al # Clear P flag
202 init.7: stosl # Set entry
203 add %edx,%eax # Next address
204 loop init.6 # Till done
207 # Bring up the system.
209 mov $0x2820,%bx # Set protected mode
210 callw setpic # IRQ offsets
211 lidt idtdesc # Set IDT
213 xor %eax,%eax # Set base
214 mov $MEM_DIR>>0x8,%ah # of page
215 mov %eax,%cr3 # directory
217 lgdt gdtdesc # Set GDT
218 mov %cr0,%eax # Switch to protected
220 or $0x80000001,%eax # mode and enable paging
225 ljmp $SEL_SCODE,$init.8 # To 32-bit code
227 init.8: xorl %ecx,%ecx # Zero
228 movb $SEL_SDATA,%cl # To 32-bit
233 movb $SEL_TSS,%cl # Set task
235 movl $MEM_USR,%edx # User base address
236 movzwl %ss:BDA_MEM,%eax # Get free memory
240 shll $0x11,%eax # To bytes
242 shll $0xa,%eax # To bytes
244 subl $0x1000,%eax # Less arg space
245 subl %edx,%eax # Less base
246 movb $SEL_UDATA,%cl # User data selector
249 push $0x202 # Set flags (IF set)
250 push $SEL_UCODE # Set CS
251 pushl btx_hdr+0xc # Set EIP
257 movb $0x7,%cl # Set remaining
258 init.9: push $0x0 # general
259 loop init.9 # registers
260 popa # and initialize
261 popl %es # Initialize
269 exit: cli # Disable interrupts
270 movl $MEM_ESP0,%esp # Clear stack
274 movl %cr0,%eax # Get CR0
276 andl $~0x80000000,%eax # Disable
277 movl %eax,%cr0 # paging
279 xorl %ecx,%ecx # Zero
281 movl %ecx,%cr3 # Flush TLB
286 ljmpw $SEL_RCODE,$exit.1 # Reload CS
288 exit.1: mov $SEL_RDATA,%cl # 16-bit selector
289 mov %cx,%ss # Reload SS
291 mov %cx,%es # remaining
292 mov %cx,%fs # segment
293 mov %cx,%gs # registers
295 # To real-address mode.
298 mov %eax,%cr0 # real mode
299 ljmp $0x0,$exit.2 # Reload CS
300 exit.2: xor %ax,%ax # Real mode segment
301 mov %ax,%ss # Reload SS
302 mov %ax,%ds # Address data
304 mov $0x1008,%bx # Set real mode
306 mov $0x7008,%bx # Set real mode
308 callw setpic # IRQ offsets
309 lidt ivtdesc # Set IVT
311 # Reboot or await reset.
313 sti # Enable interrupts
314 testb $0x1,btx_hdr+0x7 # Reboot?
315 exit.3: jz exit.3 # No
323 movw $0x1234, BDA_BOOT # Do a warm boot
324 ljmp $0xffff,$0x0 # reboot the machine
327 # Set IRQ offsets by reprogramming 8259A PICs.
330 setpic: in $0x02,%al # Save master
332 in $0x0a,%al # Save slave
334 movb $0x11,%al # ICW1 to
335 outb %al,$0x00 # master,
336 outb %al,$0x08 # slave
337 movb %bl,%al # ICW2 to
338 outb %al,$0x02 # master
339 movb %bh,%al # ICW2 to
340 outb %al,$0x0a # slave
341 movb $0x80,%al # ICW3 to
342 outb %al,$0x02 # master
343 movb $0x7,%al # ICW3 to
344 outb %al,$0x0a # slave
345 movb $0x1d,%al # ICW4 to
346 outb %al,$0x02 # master,
347 movb $0x9,%al # ICW4 to
348 outb %al,$0x0a # slave
349 pop %ax # Restore slave
351 pop %ax # Restore master
354 setpic: in $0x21,%al # Save master
356 in $0xa1,%al # Save slave
358 movb $0x11,%al # ICW1 to
359 outb %al,$0x20 # master,
360 outb %al,$0xa0 # slave
361 movb %bl,%al # ICW2 to
362 outb %al,$0x21 # master
363 movb %bh,%al # ICW2 to
364 outb %al,$0xa1 # slave
365 movb $0x4,%al # ICW3 to
366 outb %al,$0x21 # master
367 movb $0x2,%al # ICW3 to
368 outb %al,$0xa1 # slave
369 movb $0x1,%al # ICW4 to
370 outb %al,$0x21 # master,
371 outb %al,$0xa1 # slave
372 pop %ax # Restore slave
374 pop %ax # Restore master
380 # Initiate return from V86 mode to user mode.
382 inthlt: hlt # To supervisor mode
384 # Exception jump table.
386 intx00: push $0x0 # Int 0x0: #DE
387 jmp ex_noc # Divide error
388 push $0x1 # Int 0x1: #DB
390 push $0x3 # Int 0x3: #BP
391 jmp ex_noc # Breakpoint
392 push $0x4 # Int 0x4: #OF
393 jmp ex_noc # Overflow
394 push $0x5 # Int 0x5: #BR
395 jmp ex_noc # BOUND range exceeded
396 push $0x6 # Int 0x6: #UD
397 jmp ex_noc # Invalid opcode
398 push $0x7 # Int 0x7: #NM
399 jmp ex_noc # Device not available
400 push $0x8 # Int 0x8: #DF
401 jmp except # Double fault
402 push $0xa # Int 0xa: #TS
403 jmp except # Invalid TSS
404 push $0xb # Int 0xb: #NP
405 jmp except # Segment not present
406 push $0xc # Int 0xc: #SS
407 jmp except # Stack segment fault
408 push $0xd # Int 0xd: #GP
409 jmp ex_v86 # General protection
410 push $0xe # Int 0xe: #PF
411 jmp except # Page fault
412 intx10: push $0x10 # Int 0x10: #MF
413 jmp ex_noc # Floating-point error
415 # Handle #GP exception.
417 ex_v86: testb $0x2,0x12(%esp,1) # V86 mode?
419 jmp v86mon # To monitor
421 # Save a zero error code.
423 ex_noc: pushl (%esp,1) # Duplicate int no
424 movb $0x0,0x4(%esp,1) # Fake error code
428 except: cld # String ops inc
432 movb $0x6,%al # Push loop count
433 testb $0x2,0x3a(%esp,1) # V86 mode?
439 movb $0x2,%al # Push loop count
440 cmpw $SEL_SCODE,0x44(%esp,1) # Supervisor mode?
443 leal 0x50(%esp,1),%eax # Set
445 jmp except.2 # Join common code
446 except.1: pushl 0x50(%esp,1) # Set GS, FS, DS, ES
447 decb %al # (if V86 mode), and
448 jne except.1 # SS, ESP
449 except.2: push $SEL_SDATA # Set up
453 movl %esp,%ebx # Stack frame
454 movl $dmpfmt,%esi # Dump format string
455 movl $MEM_BUF,%edi # Buffer
487 call putstr # display
488 leal 0x18(%esp,1),%esp # Discard frame
492 cmpb $0x3,(%esp,1) # Breakpoint?
495 except.3: leal 0x8(%esp,1),%esp # Discard err, int no
496 iret # From interrupt
498 # Return to user mode from V86 mode.
500 intrtn: cld # String ops inc
503 leal 0x3c(%ebp),%edx # V86 Segment registers
504 movl MEM_TSS+TSS_ESP1,%esi # Link stack pointer
505 lodsl # INT_V86 args pointer
506 movl %esi,%ebx # Saved exception frame
507 testl %eax,%eax # INT_V86 args?
509 movl $MEM_USR,%edi # User base
510 movl 0x1c(%esi),%ebx # User ESP
511 movl %eax,(%edi,%ebx,1) # Restore to user stack
512 leal 0x8(%edi,%eax,1),%edi # Arg segment registers
513 testb $0x4,-0x6(%edi) # Return flags?
515 movl 0x30(%ebp),%eax # Get V86 flags
516 movw %ax,0x18(%esi) # Set user flags
517 intrtn.1: leal 0x10(%esi),%ebx # Saved exception frame
518 xchgl %edx,%esi # Segment registers
519 movb $0x4,%cl # Update seg regs
522 intrtn.2: movl %edx,%esi # Segment registers
523 leal 0x28(%ebp),%edi # Set up seg
524 movb $0x4,%cl # regs for
527 movl %ebx,%esi # Restore exception
528 movb $0x5,%cl # frame to
531 movl %esi,MEM_TSS+TSS_ESP1 # Link stack pointer
533 leal 0x8(%esp,1),%esp # Discard err, int no
542 v86mon: cld # String ops inc
543 pushl $SEL_SDATA # Set up for
544 popl %ds # flat addressing
545 pusha # Save registers
546 movl %esp,%ebp # Address stack frame
547 movzwl 0x2c(%ebp),%edi # Load V86 CS
548 shll $0x4,%edi # To linear
549 movl 0x28(%ebp),%esi # Load V86 IP
550 addl %edi,%esi # Code pointer
551 xorl %ecx,%ecx # Zero
552 movb $0x2,%cl # 16-bit operands
553 xorl %eax,%eax # Zero
554 v86mon.1: lodsb # Get opcode
555 cmpb $0x66,%al # Operand size prefix?
557 movb $0x4,%cl # 32-bit operands
558 jmp v86mon.1 # Continue
559 v86mon.2: cmpb $0xf4,%al # HLT?
561 cmpl $inthlt+0x1,%esi # Is inthlt?
562 jne v86mon.7 # No (ignore)
563 jmp intrtn # Return to user mode
564 v86mon.3: cmpb $0xf,%al # Prefixed instruction?
566 cmpb $0x09,(%esi) # Is it a WBINVD?
568 cmpb $0x30,(%esi) # Is it a WRMSR?
570 cmpb $0x32,(%esi) # Is it a RDMSR?
572 cmpb $0x20,(%esi) # Is this a
573 jne v86mon.4 # MOV EAX,CR0
574 cmpb $0xc0,0x1(%esi) # instruction?
576 v86mon.4: cmpb $0xfa,%al # CLI?
578 cmpb $0xfb,%al # STI?
580 movzwl 0x38(%ebp),%ebx # Load V86 SS
581 shll $0x4,%ebx # To offset
583 addl 0x34(%ebp),%ebx # Add V86 SP
584 movl 0x30(%ebp),%edx # Load V86 flags
585 cmpb $0x9c,%al # PUSHF/PUSHFD?
587 cmpb $0x9d,%al # POPF/POPFD?
589 cmpb $0xcd,%al # INT imm8?
591 cmpb $0xcf,%al # IRET/IRETD?
595 jmp except # Handle exception
596 v86mon.5: movl %edx,0x30(%ebp) # Save V86 flags
597 v86mon.6: popl %edx # V86 SS adjustment
598 subl %edx,%ebx # Save V86
599 movl %ebx,0x34(%ebp) # SP
600 v86mon.7: subl %edi,%esi # From linear
601 movl %esi,0x28(%ebp) # Save V86 IP
603 leal 0x8(%esp,1),%esp # Discard int no, error
606 # Emulate MOV EAX,CR0.
608 v86mov: movl %cr0,%eax # CR0 to
609 movl %eax,0x1c(%ebp) # saved EAX
610 incl %esi # Adjust IP
612 # Return from emulating a 0x0f prefixed instruction
614 v86preret: incl %esi # Adjust IP
615 jmp v86mon.7 # Finish up
619 v86wbinvd: wbinvd # Write back and invalidate
621 jmp v86preret # Finish up
625 v86wrmsr: movl 0x18(%ebp),%ecx # Get user's %ecx (MSR to write)
626 movl 0x14(%ebp),%edx # Load the value
627 movl 0x1c(%ebp),%eax # to write
629 jmp v86preret # Finish up
633 v86rdmsr: movl 0x18(%ebp),%ecx # MSR to read
635 movl %eax,0x1c(%ebp) # Return the value of
636 movl %edx,0x14(%ebp) # the MSR to the user
637 jmp v86preret # Finish up
641 v86cli: andb $~0x2,0x31(%ebp) # Clear IF
642 jmp v86mon.7 # Finish up
646 v86sti: orb $0x2,0x31(%ebp) # Set IF
647 jmp v86mon.7 # Finish up
649 # Emulate PUSHF/PUSHFD.
651 v86pushf: subl %ecx,%ebx # Adjust SP
652 cmpb $0x4,%cl # 32-bit
655 v86pushf.1: movl %edx,(%ebx) # Save flags
656 jmp v86mon.6 # Finish up
658 # Emulate IRET/IRETD.
660 v86iret: movzwl (%ebx),%esi # Load V86 IP
661 movzwl 0x2(%ebx),%edi # Load V86 CS
662 leal 0x4(%ebx),%ebx # Adjust SP
663 movl %edi,0x2c(%ebp) # Save V86 CS
664 xorl %edi,%edi # No ESI adjustment
666 # Emulate POPF/POPFD (and remainder of IRET/IRETD).
668 v86popf: cmpb $0x4,%cl # 32-bit?
670 movl %edx,%eax # Initialize
672 v86popf.1: movl (%ebx),%eax # Load flags
673 addl %ecx,%ebx # Adjust SP
674 andl $V86_FLG,%eax # Merge
675 andl $~V86_FLG,%edx # the
676 orl %eax,%edx # flags
677 jmp v86mon.5 # Finish up
679 # trap int 15, function 87
680 # reads %es:%si from saved registers on stack to find a GDT containing
681 # source and destination locations
682 # reads count of words from saved %cx
683 # returns success by setting %ah to 0
685 int15_87: pushl %eax # Save
686 pushl %ebx # some information
687 pushl %esi # onto the stack.
689 xorl %eax,%eax # clean EAX
690 xorl %ebx,%ebx # clean EBX
691 movl 0x4(%ebp),%esi # Get user's ESI
692 movl 0x3C(%ebp),%ebx # store ES
693 movw %si,%ax # store SI
694 shll $0x4,%ebx # Make it a seg.
695 addl %eax,%ebx # ebx=(es<<4)+si
696 movb 0x14(%ebx),%al # Grab the
697 movb 0x17(%ebx),%ah # necessary
698 shll $0x10,%eax # information
699 movw 0x12(%ebx),%ax # from
701 movb 0x1c(%ebx),%al # GDT in order to
702 movb 0x1f(%ebx),%ah # have %esi offset
703 shll $0x10,%eax # of source and %edi
704 movw 0x1a(%ebx),%ax # of destination.
708 pushl %ecx # stash ECX
709 xorl %ecx,%ecx # highw of ECX is clear
710 movw 0x18(%ebp),%cx # Get user's ECX
711 shll $0x1,%ecx # Convert from num words to num
714 movsb # perform copy.
720 movb $0x0,0x1d(%ebp) # set ah = 0 to indicate
722 andb $0xfe,%dl # clear CF
723 jmp v86mon.5 # Finish up
726 # Reboot the machine by setting the reboot flag and exiting
728 reboot: orb $0x1,btx_hdr+0x7 # Set the reboot flag
729 jmp exit # Terminate BTX and reboot
732 # Emulate INT imm8... also make sure to check if it's int 15/87
734 v86intn: lodsb # Get int no
735 cmpb $0x19,%al # is it int 19?
736 je reboot # yes, reboot the machine
737 cmpb $0x15,%al # is it int 15?
738 jne v86intn.3 # no, skip parse
739 pushl %eax # stash EAX
740 movl 0x1c(%ebp),%eax # user's saved EAX
741 cmpb $0x87,%ah # is it the memcpy subfunction?
742 jne v86intn.1 # no, keep checking
743 popl %eax # get the stack straight
744 jmp int15_87 # it's our cue
745 v86intn.1: cmpw $0x4f53,%ax # is it the delete key callout?
746 jne v86intn.2 # no, handle the int normally
747 movb BDA_KEYFLAGS,%al # get the shift key state
749 andb $0x18,%al # mask off just Ctrl and Alt
750 cmpb $0x18,%al # are both Ctrl and Alt down?
752 andb $0xc,%al # mask off just Ctrl and Alt
753 cmpb $0xc,%al # are both Ctrl and Alt down?
755 jne v86intn.2 # no, handle the int normally
756 popl %eax # restore EAX
757 jmp reboot # reboot the machine
758 v86intn.2: popl %eax # restore EAX
759 v86intn.3: subl %edi,%esi # From
760 shrl $0x4,%edi # linear
761 movw %dx,-0x2(%ebx) # Save flags
762 movw %di,-0x4(%ebx) # Save CS
763 leal -0x6(%ebx),%ebx # Adjust SP
764 movw %si,(%ebx) # Save IP
765 shll $0x2,%eax # Scale
766 movzwl (%eax),%esi # Load IP
767 movzwl 0x2(%eax),%edi # Load CS
768 movl %edi,0x2c(%ebp) # Save CS
769 xorl %edi,%edi # No ESI adjustment
770 andb $~0x1,%dh # Clear TF
771 jmp v86mon.5 # Finish up
773 # Hardware interrupt jump table.
775 intx20: push $0x8 # Int 0x20: IRQ0
776 jmp int_hw # V86 int 0x8
777 push $0x9 # Int 0x21: IRQ1
778 jmp int_hw # V86 int 0x9
779 push $0xa # Int 0x22: IRQ2
780 jmp int_hw # V86 int 0xa
781 push $0xb # Int 0x23: IRQ3
782 jmp int_hw # V86 int 0xb
783 push $0xc # Int 0x24: IRQ4
784 jmp int_hw # V86 int 0xc
785 push $0xd # Int 0x25: IRQ5
786 jmp int_hw # V86 int 0xd
787 push $0xe # Int 0x26: IRQ6
788 jmp int_hw # V86 int 0xe
789 push $0xf # Int 0x27: IRQ7
790 jmp int_hw # V86 int 0xf
792 push $0x10 # Int 0x28: IRQ8
793 jmp int_hw # V86 int 0x10
794 push $0x11 # Int 0x29: IRQ9
795 jmp int_hw # V86 int 0x11
796 push $0x12 # Int 0x2a: IRQ10
797 jmp int_hw # V86 int 0x12
798 push $0x13 # Int 0x2b: IRQ11
799 jmp int_hw # V86 int 0x13
800 push $0x14 # Int 0x2c: IRQ12
801 jmp int_hw # V86 int 0x14
802 push $0x15 # Int 0x2d: IRQ13
803 jmp int_hw # V86 int 0x15
804 push $0x16 # Int 0x2e: IRQ14
805 jmp int_hw # V86 int 0x16
806 push $0x17 # Int 0x2f: IRQ15
807 jmp int_hw # V86 int 0x17
809 push $0x70 # Int 0x28: IRQ8
810 jmp int_hw # V86 int 0x70
811 push $0x71 # Int 0x29: IRQ9
812 jmp int_hw # V86 int 0x71
813 push $0x72 # Int 0x2a: IRQ10
814 jmp int_hw # V86 int 0x72
815 push $0x73 # Int 0x2b: IRQ11
816 jmp int_hw # V86 int 0x73
817 push $0x74 # Int 0x2c: IRQ12
818 jmp int_hw # V86 int 0x74
819 push $0x75 # Int 0x2d: IRQ13
820 jmp int_hw # V86 int 0x75
821 push $0x76 # Int 0x2e: IRQ14
822 jmp int_hw # V86 int 0x76
823 push $0x77 # Int 0x2f: IRQ15
824 jmp int_hw # V86 int 0x77
827 # Reflect hardware interrupts.
829 int_hw: testb $0x2,0xe(%esp,1) # V86 mode?
831 pushl $SEL_SDATA # Address
833 xchgl %eax,(%esp,1) # Swap EAX, int no
835 movl %esp,%ebp # stack frame
837 shll $0x2,%eax # Get int
838 movl (%eax),%eax # vector
839 subl $0x6,0x14(%ebp) # Adjust V86 ESP
840 movzwl 0x18(%ebp),%ebx # V86 SS
841 shll $0x4,%ebx # * 0x10
842 addl 0x14(%ebp),%ebx # + V86 ESP
843 xchgw %ax,0x8(%ebp) # Swap V86 IP
844 rorl $0x10,%eax # Swap words
845 xchgw %ax,0xc(%ebp) # Swap V86 CS
846 roll $0x10,%eax # Swap words
847 movl %eax,(%ebx) # CS:IP for IRET
848 movl 0x10(%ebp),%eax # V86 flags
849 movw %ax,0x4(%ebx) # Flags for IRET
850 andb $~0x3,0x11(%ebp) # Clear IF, TF
853 popl %eax # registers
856 # Invoke V86 interrupt from user mode, with arguments.
858 intx31: stc # Have btx_v86
859 pushl %eax # Missing int no
861 # Invoke V86 interrupt from user mode.
863 intusr: std # String ops dec
869 movl %esp,%eax # seg regs
873 push $SEL_SDATA # Set up
877 movl $MEM_USR,%ebx # User base
878 movl %ebx,%edx # address
879 jc intusr.1 # If btx_v86
880 xorl %edx,%edx # Control flags
881 xorl %ebp,%ebp # btx_v86 pointer
882 intusr.1: leal 0x50(%esp,1),%esi # Base of frame
884 addl -0x4(%esi),%ebx # User ESP
885 movl MEM_TSS+TSS_ESP1,%edi # Link stack pointer
886 leal -0x4(%edi),%edi # Adjust for push
887 xorl %ecx,%ecx # Zero
888 movb $0x5,%cl # Push exception
891 xchgl %eax,%esi # Saved seg regs
892 movl 0x40(%esp,1),%eax # Get int no
893 testl %edx,%edx # Have btx_v86?
895 movl (%ebx),%ebp # btx_v86 pointer
896 movb $0x4,%cl # Count
897 addl %ecx,%ebx # Adjust for pop
898 rep # Push saved seg regs
899 movsl # on link stack
900 addl %ebp,%edx # Flatten btx_v86 ptr
901 leal 0x14(%edx),%esi # Seg regs pointer
902 movl 0x4(%edx),%eax # Get int no/address
903 movzwl 0x2(%edx),%edx # Get control flags
904 intusr.2: movl %ebp,(%edi) # Push btx_v86 and
905 movl %edi,MEM_TSS+TSS_ESP1 # save link stack ptr
906 popl %edi # Base of frame
907 xchgl %eax,%ebp # Save intno/address
908 movl 0x48(%esp,1),%eax # Get flags
909 testb $0x2,%dl # Simulate CALLF?
911 decl %ebx # Push flags
913 movw %ax,(%ebx) # stack
914 intusr.3: movb $0x4,%cl # Count
915 subl %ecx,%ebx # Push return address
916 movl $inthlt,(%ebx) # on V86 stack
917 rep # Copy seg regs to
918 movsl # exception frame
919 xchgl %eax,%ecx # Save flags
920 movl %ebx,%eax # User ESP
921 subl $V86_STK,%eax # Less bytes
923 xorl %eax,%eax # keep
924 intusr.4: shrl $0x4,%eax # Gives segment
926 shll $0x4,%eax # To bytes
927 xchgl %eax,%ebx # Swap
928 subl %ebx,%eax # Gives offset
930 xchgl %eax,%ecx # Get flags
931 btsl $0x11,%eax # Set VM
932 andb $~0x1,%ah # Clear TF
934 xchgl %eax,%ebp # Get int no/address
935 testb $0x1,%dl # Address?
937 shll $0x2,%eax # Scale
938 movl (%eax),%eax # Load int vector
939 intusr.5: movl %eax,%ecx # Save
940 shrl $0x10,%eax # Gives segment
942 movw %cx,%ax # Restore
944 leal 0x10(%esp,1),%esp # Discard seg regs
950 intx30: cmpl $SYS_EXEC,%eax # Exec system call?
960 movl $MEM_USR,%eax # User base address
961 addl 0xc(%esp,1),%eax # Change to user
962 leal 0x4(%eax),%esp # stack
964 movl %cr0,%eax # Turn
965 andl $~0x80000000,%eax # off
966 movl %eax,%cr0 # paging
967 xorl %eax,%eax # Flush
972 intx30.1: incb %ss:btx_hdr+0x7 # Flag reboot
975 # Dump structure [EBX] to [EDI], using format string [ESI].
977 dump.0: stosb # Save char
978 dump: lodsb # Load char
979 testb %al,%al # End of string?
981 testb $0x80,%al # Control?
983 movb %al,%ch # Save control
984 movb $'=',%al # Append
989 addl %ebx,%esi # pointer
990 testb $DMP_X16,%ch # Dump word?
994 dump.1: testb $DMP_X32,%ch # Dump long?
998 dump.2: testb $DMP_MEM,%ch # Dump memory?
1001 testb $0x2,0x52(%ebx) # V86 mode?
1003 verr 0x4(%esi) # Readable selector?
1005 ldsl (%esi),%esi # Load pointer
1006 jmp dump.4 # Join common code
1007 dump.3: lodsl # Set offset
1008 xchgl %eax,%edx # Save
1010 shll $0x4,%eax # * 0x10
1011 addl %edx,%eax # + offset
1012 xchgl %eax,%esi # Set pointer
1013 dump.4: movb $0x10,%cl # Bytes to dump
1014 dump.5: lodsb # Get byte and
1016 decb %cl # Keep count
1018 movb $'-',%al # Separator
1019 cmpb $0x8,%cl # Half way?
1021 movb $' ',%al # Use space
1022 dump.6: stosb # Save separator
1023 jmp dump.5 # Continue
1024 dump.7: popl %ds # Restore
1025 dump.8: popl %esi # Restore
1026 movb $0xa,%al # Line feed
1027 testb $DMP_EOL,%ch # End of line?
1029 movb $' ',%al # Use spaces
1031 dump.9: jmp dump.0 # Continue
1032 dump.10: stosb # Terminate string
1035 # Convert EAX, AX, or AL to hex, saving the result to [EDI].
1037 hex32: pushl %eax # Save
1038 shrl $0x10,%eax # Do upper
1041 hex16: call hex16.1 # Do upper 8
1042 hex16.1: xchgb %ah,%al # Save/restore
1043 hex8: pushl %eax # Save
1044 shrb $0x4,%al # Do upper
1047 hex8.1: andb $0xf,%al # Get lower 4
1048 cmpb $0xa,%al # Convert
1049 sbbb $0x69,%al # to hex
1051 orb $0x20,%al # To lower case
1055 # Output zero-terminated string [ESI] to the console.
1057 putstr.0: call putchr # Output char
1058 putstr: lodsb # Load char
1059 testb %al,%al # End of string?
1063 # Output character AL to the console.
1065 putchr: pusha # Save
1066 xorl %ecx,%ecx # Zero for loops
1067 movb $SCR_MAT,%ah # Mode/attribute
1068 movl $BDA_POS,%ebx # BDA pointer
1069 movw (%ebx),%dx # Cursor position
1073 movl $0xb8000,%edi # Regen buffer (color)
1074 cmpb %ah,BDA_SCR-BDA_POS(%ebx) # Mono mode?
1076 xorw %di,%di # Regen buffer (mono)
1078 putchr.1: cmpb $0xa,%al # New line?
1082 movb %al,(%edi,%ecx,1) # Write char
1084 movb %ah,(%edi,%ecx,1) # Write attr
1087 putchr.2: movw %dx,%ax
1093 putchr.3: cmpw $SCR_ROW*SCR_COL*2,%dx
1095 xchgl %eax,%ecx # Save char
1096 movb $SCR_COL,%al # Columns per row
1097 mulb %dh # * row position
1098 addb %dl,%al # + column
1099 adcb $0x0,%ah # position
1101 xchgl %eax,%ecx # Swap char, offset
1102 movw %ax,(%edi,%ecx,1) # Write attr:char
1103 incl %edx # Bump cursor
1104 cmpb $SCR_COL,%dl # Beyond row?
1106 putchr.2: xorb %dl,%dl # Zero column
1108 putchr.3: cmpb $SCR_ROW,%dh # Beyond screen?
1111 leal 2*SCR_COL(%edi),%esi # New top line
1112 movw $(SCR_ROW-1)*SCR_COL/2,%cx # Words to move
1115 movb $' ',%al # Space
1119 movb $SCR_COL,%cl # Columns to clear
1123 movw $(SCR_ROW-1)*SCR_COL*2,%dx
1125 movb $SCR_ROW-1,%dh # Bottom line
1127 putchr.4: movw %dx,(%ebx) # Update position
1133 # Global descriptor table.
1135 gdt: .word 0x0,0x0,0x0,0x0 # Null entry
1136 .word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE
1137 .word 0xffff,0x0,0x9200,0xcf # SEL_SDATA
1138 .word 0xffff,0x0,0x9a00,0x0 # SEL_RCODE
1139 .word 0xffff,0x0,0x9200,0x0 # SEL_RDATA
1140 .word 0xffff,MEM_USR,0xfa00,0xcf# SEL_UCODE
1141 .word 0xffff,MEM_USR,0xf200,0xcf# SEL_UDATA
1142 .word _TSSLM,MEM_TSS,0x8900,0x0 # SEL_TSS
1145 # Pseudo-descriptors.
1147 gdtdesc: .word gdt.1-gdt-1,gdt,0x0 # GDT
1148 idtdesc: .word _IDTLM,MEM_IDT,0x0 # IDT
1149 ivtdesc: .word 0x400-0x0-1,0x0,0x0 # IVT
1151 # IDT construction control string.
1153 idtctl: .byte 0x10, 0x8e # Int 0x0-0xf
1154 .word 0x7dfb,intx00 # (exceptions)
1155 .byte 0x10, 0x8e # Int 0x10
1156 .word 0x1, intx10 # (exception)
1157 .byte 0x10, 0x8e # Int 0x20-0x2f
1158 .word 0xffff,intx20 # (hardware)
1159 .byte 0x1, 0xee # int 0x30
1160 .word 0x1, intx30 # (system call)
1161 .byte 0x2, 0xee # Int 0x31-0x32
1162 .word 0x1, intx31 # (V86, null)
1163 .byte 0x0 # End of string
1165 # Dump format string.
1167 dmpfmt: .byte '\n' # "\n"
1168 .ascii "int" # "int="
1169 .byte 0x80|DMP_X32, 0x40 # "00000000 "
1170 .ascii "err" # "err="
1171 .byte 0x80|DMP_X32, 0x44 # "00000000 "
1172 .ascii "efl" # "efl="
1173 .byte 0x80|DMP_X32, 0x50 # "00000000 "
1174 .ascii "eip" # "eip="
1175 .byte 0x80|DMP_X32|DMP_EOL,0x48 # "00000000\n"
1176 .ascii "eax" # "eax="
1177 .byte 0x80|DMP_X32, 0x34 # "00000000 "
1178 .ascii "ebx" # "ebx="
1179 .byte 0x80|DMP_X32, 0x28 # "00000000 "
1180 .ascii "ecx" # "ecx="
1181 .byte 0x80|DMP_X32, 0x30 # "00000000 "
1182 .ascii "edx" # "edx="
1183 .byte 0x80|DMP_X32|DMP_EOL,0x2c # "00000000\n"
1184 .ascii "esi" # "esi="
1185 .byte 0x80|DMP_X32, 0x1c # "00000000 "
1186 .ascii "edi" # "edi="
1187 .byte 0x80|DMP_X32, 0x18 # "00000000 "
1188 .ascii "ebp" # "ebp="
1189 .byte 0x80|DMP_X32, 0x20 # "00000000 "
1190 .ascii "esp" # "esp="
1191 .byte 0x80|DMP_X32|DMP_EOL,0x0 # "00000000\n"
1193 .byte 0x80|DMP_X16, 0x4c # "0000 "
1195 .byte 0x80|DMP_X16, 0xc # "0000 "
1197 .byte 0x80|DMP_X16, 0x8 # "0000 "
1200 .byte 0x80|DMP_X16, 0x10 # "0000 "
1202 .byte 0x80|DMP_X16, 0x14 # "0000 "
1204 .byte 0x80|DMP_X16|DMP_EOL,0x4 # "0000\n"
1205 .ascii "cs:eip" # "cs:eip="
1206 .byte 0x80|DMP_MEM|DMP_EOL,0x48 # "00 00 ... 00 00\n"
1207 .ascii "ss:esp" # "ss:esp="
1208 .byte 0x80|DMP_MEM|DMP_EOL,0x0 # "00 00 ... 00 00\n"
1209 .asciz "BTX halted" # End
1211 # End of BTX memory.