btx.S revision 138189
1128710Sru/* 2128710Sru * Copyright (c) 1998 Robert Nordier 3128710Sru * All rights reserved. 4128710Sru * 5128710Sru * Redistribution and use in source and binary forms are freely 6128710Sru * permitted provided that the above copyright notice and this 7128710Sru * paragraph and the following disclaimer are duplicated in all 8128710Sru * such forms. 9128710Sru * 10128710Sru * This software is provided "AS IS" and without any express or 11128710Sru * implied warranties, including, without limitation, the implied 12128710Sru * warranties of merchantability and fitness for a particular 13128710Sru * purpose. 14128710Sru * 15128710Sru * $FreeBSD: head/sys/boot/pc98/btx/btx/btx.S 138189 2004-11-29 11:54:42Z nyan $ 16128710Sru */ 1743561Skato 18128710Sru/* 19128710Sru * Memory layout. 20128710Sru */ 2143561Skato .set MEM_BTX,0x1000 # Start of BTX memory 2243561Skato .set MEM_ESP0,0x1800 # Supervisor stack 2343561Skato .set MEM_BUF,0x1800 # Scratch buffer 2443561Skato .set MEM_ESP1,0x1e00 # Link stack 2543561Skato .set MEM_IDT,0x1e00 # IDT 2643561Skato .set MEM_TSS,0x1f98 # TSS 2743561Skato .set MEM_MAP,0x2000 # I/O bit map 2843561Skato .set MEM_DIR,0x4000 # Page directory 2943561Skato .set MEM_TBL,0x5000 # Page tables 3043561Skato .set MEM_ORG,0x9000 # BTX code 3143561Skato .set MEM_USR,0xa000 # Start of user memory 32128710Sru/* 33128710Sru * Paging control. 34128710Sru */ 3543561Skato .set PAG_SIZ,0x1000 # Page size 3643561Skato .set PAG_CNT,0x1000 # Pages to map 37128710Sru/* 38128710Sru * Segment selectors. 39128710Sru */ 4043561Skato .set SEL_SCODE,0x8 # Supervisor code 4143561Skato .set SEL_SDATA,0x10 # Supervisor data 4243561Skato .set SEL_RCODE,0x18 # Real mode code 4343561Skato .set SEL_RDATA,0x20 # Real mode data 4443561Skato .set SEL_UCODE,0x28|3 # User code 4543561Skato .set SEL_UDATA,0x30|3 # User data 4643561Skato .set SEL_TSS,0x38 # TSS 47128710Sru/* 48128710Sru * Task state segment fields. 49128710Sru */ 5043561Skato .set TSS_ESP0,0x4 # PL 0 ESP 5143561Skato .set TSS_SS0,0x8 # PL 0 SS 5243561Skato .set TSS_ESP1,0xc # PL 1 ESP 5343561Skato .set TSS_MAP,0x66 # I/O bit map base 54128710Sru/* 55128710Sru * System calls. 56128710Sru */ 5743561Skato .set SYS_EXIT,0x0 # Exit 5843561Skato .set SYS_EXEC,0x1 # Exec 59128710Sru/* 60128710Sru * V86 constants. 61128710Sru */ 6243561Skato .set V86_FLG,0x208eff # V86 flag mask 6343561Skato .set V86_STK,0x400 # V86 stack allowance 64128710Sru/* 65128710Sru * Dump format control bytes. 66128710Sru */ 6743561Skato .set DMP_X16,0x1 # Word 6843561Skato .set DMP_X32,0x2 # Long 6943561Skato .set DMP_MEM,0x4 # Memory 7043561Skato .set DMP_EOL,0x8 # End of line 71128710Sru/* 72128710Sru * Screen defaults and assumptions. 73128710Sru */ 74125780Snyan#ifdef PC98 7543561Skato .set SCR_MAT,0xe1 # Mode/attribute 76125780Snyan#else 7743561Skato .set SCR_MAT,0x7 # Mode/attribute 78125780Snyan#endif 7943561Skato .set SCR_COL,0x50 # Columns per row 8043561Skato .set SCR_ROW,0x19 # Rows per screen 81128710Sru/* 82128710Sru * BIOS Data Area locations. 83128710Sru */ 84125780Snyan#ifdef PC98 8558871Skato .set BDA_MEM,0x501 # Free memory 8658871Skato .set BDA_KEYFLAGS,0x53a # Keyboard shift-state flags 8758871Skato .set BDA_POS,0x53e # Cursor position 88125780Snyan#else 8943561Skato .set BDA_MEM,0x413 # Free memory 9058871Skato .set BDA_KEYFLAGS,0x417 # Keyboard shift-state flags 9143561Skato .set BDA_SCR,0x449 # Video mode 9243561Skato .set BDA_POS,0x450 # Cursor position 9358871Skato .set BDA_BOOT,0x472 # Boot howto flag 94125780Snyan#endif 95128710Sru/* 96128710Sru * Derivations, for brevity. 97128710Sru */ 9843561Skato .set _ESP0H,MEM_ESP0>>0x8 # Byte 1 of ESP0 9943561Skato .set _ESP1H,MEM_ESP1>>0x8 # Byte 1 of ESP1 10043561Skato .set _TSSIO,MEM_MAP-MEM_TSS # TSS I/O base 10143561Skato .set _TSSLM,MEM_DIR-MEM_TSS-1 # TSS limit 10243561Skato .set _IDTLM,MEM_TSS-MEM_IDT-1 # IDT limit 103128710Sru/* 104128710Sru * Code segment. 105128710Sru */ 10643561Skato .globl start 10761064Snyan .code16 10843561Skatostart: # Start of code 109128710Sru/* 110128710Sru * BTX header. 111128710Sru */ 11243561Skatobtx_hdr: .byte 0xeb # Machine ID 11343561Skato .byte 0xe # Header size 11443561Skato .ascii "BTX" # Magic 11543561Skato .byte 0x1 # Major version 11644368Skato .byte 0x1 # Minor version 11768358Snyan .byte BTX_FLAGS # Flags 11843561Skato .word PAG_CNT-MEM_ORG>>0xc # Paging control 11943561Skato .word break-start # Text size 12043561Skato .long 0x0 # Entry address 121128710Sru/* 122128710Sru * Initialization routine. 123128710Sru */ 12443561Skatoinit: cli # Disable interrupts 12561064Snyan xor %ax,%ax # Zero/segment 12661064Snyan mov %ax,%ss # Set up 12761064Snyan mov $MEM_ESP0,%sp # stack 12861064Snyan mov %ax,%es # Address 12961064Snyan mov %ax,%ds # data 13061064Snyan pushl $0x2 # Clear 13161064Snyan popfl # flags 132128710Sru/* 133128710Sru * Initialize memory. 134128710Sru */ 13561064Snyan mov $MEM_IDT,%di # Memory to initialize 13661064Snyan mov $(MEM_ORG-MEM_IDT)/2,%cx # Words to zero 13761064Snyan push %di # Save 13843561Skato rep # Zero-fill 13961064Snyan stosw # memory 14061064Snyan pop %di # Restore 141128710Sru/* 142128710Sru * Create IDT. 143128710Sru */ 14461064Snyan mov $idtctl,%si # Control string 14543561Skatoinit.1: lodsb # Get entry 14661064Snyan cbw # count 14761064Snyan xchg %ax,%cx # as word 14861064Snyan jcxz init.4 # If done 14943561Skato lodsb # Get segment 15061064Snyan xchg %ax,%dx # P:DPL:type 15161064Snyan lodsw # Get control 15261064Snyan xchg %ax,%bx # set 15361064Snyan lodsw # Get handler offset 15461064Snyan mov $SEL_SCODE,%dh # Segment selector 15561064Snyaninit.2: shr %bx # Handle this int? 15643561Skato jnc init.3 # No 15761064Snyan mov %ax,(%di) # Set handler offset 15861064Snyan mov %dh,0x2(%di) # and selector 15961064Snyan mov %dl,0x5(%di) # Set P:DPL:type 16061064Snyan add $0x4,%ax # Next handler 16161064Snyaninit.3: lea 0x8(%di),%di # Next entry 16243561Skato loop init.2 # Till set done 16343561Skato jmp init.1 # Continue 164128710Sru/* 165128710Sru * Initialize TSS. 166128710Sru */ 16761064Snyaninit.4: movb $_ESP0H,TSS_ESP0+1(%di) # Set ESP0 16861064Snyan movb $SEL_SDATA,TSS_SS0(%di) # Set SS0 16961064Snyan movb $_ESP1H,TSS_ESP1+1(%di) # Set ESP1 17061064Snyan movb $_TSSIO,TSS_MAP(%di) # Set I/O bit map base 171125780Snyan#ifdef PAGING 172128710Sru/* 173128710Sru * Create page directory. 174128710Sru */ 17561064Snyan xor %edx,%edx # Page 17661064Snyan mov $PAG_SIZ>>0x8,%dh # size 17761064Snyan xor %eax,%eax # Zero 17861064Snyan mov $MEM_DIR,%di # Page directory 17961064Snyan mov $PAG_CNT>>0xa,%cl # Entries 18061064Snyan mov $MEM_TBL|0x7,%ax # First entry 18161064Snyaninit.5: stosl # Write entry 18261064Snyan add %dx,%ax # To next 18343561Skato loop init.5 # Till done 184128710Sru/* 185128710Sru * Create page tables. 186128710Sru */ 18761064Snyan mov $MEM_TBL,%di # Page table 18861064Snyan mov $PAG_CNT>>0x8,%ch # Entries 18961064Snyan xor %ax,%ax # Start address 19061064Snyaninit.6: mov $0x7,%al # Set U:W:P flags 19161064Snyan cmp btx_hdr+0x8,%cx # Standard user page? 19243561Skato jb init.7 # Yes 19361064Snyan cmp $PAG_CNT-MEM_BTX>>0xc,%cx # BTX memory? 19443561Skato jae init.7 # No or first page 19561064Snyan and $~0x2,%al # Clear W flag 19661064Snyan cmp $PAG_CNT-MEM_USR>>0xc,%cx # User page zero? 19743561Skato jne init.7 # No 19861064Snyan testb $0x80,btx_hdr+0x7 # Unmap it? 19943561Skato jz init.7 # No 20061064Snyan and $~0x1,%al # Clear P flag 20161064Snyaninit.7: stosl # Set entry 20261064Snyan add %edx,%eax # Next address 20343561Skato loop init.6 # Till done 204125780Snyan#endif 205128710Sru/* 206128710Sru * Bring up the system. 207128710Sru */ 20861064Snyan mov $0x2820,%bx # Set protected mode 20961064Snyan callw setpic # IRQ offsets 21061064Snyan lidt idtdesc # Set IDT 211125780Snyan#ifdef PAGING 21261064Snyan xor %eax,%eax # Set base 21361064Snyan mov $MEM_DIR>>0x8,%ah # of page 21461064Snyan mov %eax,%cr3 # directory 215125780Snyan#endif 21661064Snyan lgdt gdtdesc # Set GDT 21761064Snyan mov %cr0,%eax # Switch to protected 218125780Snyan#ifdef PAGING 21961064Snyan or $0x80000001,%eax # mode and enable paging 220125780Snyan#else 22161064Snyan or $0x01,%eax # mode 222125780Snyan#endif 223128710Sru mov %eax,%cr0 # 22461064Snyan ljmp $SEL_SCODE,$init.8 # To 32-bit code 22561064Snyan .code32 22643561Skatoinit.8: xorl %ecx,%ecx # Zero 22743561Skato movb $SEL_SDATA,%cl # To 32-bit 22861064Snyan movw %cx,%ss # stack 229128710Sru/* 230128710Sru * Launch user task. 231128710Sru */ 23243561Skato movb $SEL_TSS,%cl # Set task 23361064Snyan ltr %cx # register 23443561Skato movl $MEM_USR,%edx # User base address 23543561Skato movzwl %ss:BDA_MEM,%eax # Get free memory 236125780Snyan#ifdef PC98 23743561Skato andl $0x7,%eax 23843561Skato incl %eax 23943561Skato shll $0x11,%eax # To bytes 240125780Snyan#else 24143561Skato shll $0xa,%eax # To bytes 242125780Snyan#endif 24343561Skato subl $0x1000,%eax # Less arg space 24443561Skato subl %edx,%eax # Less base 24543561Skato movb $SEL_UDATA,%cl # User data selector 24643561Skato pushl %ecx # Set SS 24743561Skato pushl %eax # Set ESP 24861064Snyan push $0x202 # Set flags (IF set) 24961064Snyan push $SEL_UCODE # Set CS 25043561Skato pushl btx_hdr+0xc # Set EIP 25143561Skato pushl %ecx # Set GS 25243561Skato pushl %ecx # Set FS 25343561Skato pushl %ecx # Set DS 25443561Skato pushl %ecx # Set ES 25543561Skato pushl %edx # Set EAX 25643561Skato movb $0x7,%cl # Set remaining 25761064Snyaninit.9: push $0x0 # general 25843561Skato loop init.9 # registers 259125780Snyan#ifdef BTX_SERIAL 26086497Snyan call sio_init # setup the serial console 261125780Snyan#endif 26243561Skato popa # and initialize 26343561Skato popl %es # Initialize 26443561Skato popl %ds # user 26543561Skato popl %fs # segment 26643561Skato popl %gs # registers 26743561Skato iret # To user mode 268128710Sru/* 269128710Sru * Exit routine. 270128710Sru */ 27143561Skatoexit: cli # Disable interrupts 27243561Skato movl $MEM_ESP0,%esp # Clear stack 273128710Sru/* 274128710Sru * Turn off paging. 275128710Sru */ 27643561Skato movl %cr0,%eax # Get CR0 27743561Skato andl $~0x80000000,%eax # Disable 27843561Skato movl %eax,%cr0 # paging 27943561Skato xorl %ecx,%ecx # Zero 28043561Skato movl %ecx,%cr3 # Flush TLB 281128710Sru/* 282128710Sru * Restore the GDT in case we caught a kernel trap. 283128710Sru */ 28476927Snyan lgdt gdtdesc # Set GDT 285128710Sru/* 286128710Sru * To 16 bits. 287128710Sru */ 28861064Snyan ljmpw $SEL_RCODE,$exit.1 # Reload CS 28961064Snyan .code16 29061064Snyanexit.1: mov $SEL_RDATA,%cl # 16-bit selector 29161064Snyan mov %cx,%ss # Reload SS 29261064Snyan mov %cx,%ds # Load 29361064Snyan mov %cx,%es # remaining 29461064Snyan mov %cx,%fs # segment 29561064Snyan mov %cx,%gs # registers 296128710Sru/* 297128710Sru * To real-address mode. 298128710Sru */ 29961064Snyan dec %ax # Switch to 30061064Snyan mov %eax,%cr0 # real mode 30161064Snyan ljmp $0x0,$exit.2 # Reload CS 30261064Snyanexit.2: xor %ax,%ax # Real mode segment 30361064Snyan mov %ax,%ss # Reload SS 30461064Snyan mov %ax,%ds # Address data 305125780Snyan#ifdef PC98 30661064Snyan mov $0x1008,%bx # Set real mode 307125780Snyan#else 30861064Snyan mov $0x7008,%bx # Set real mode 309125780Snyan#endif 31061064Snyan callw setpic # IRQ offsets 31161064Snyan lidt ivtdesc # Set IVT 312128710Sru/* 313128710Sru * Reboot or await reset. 314128710Sru */ 31543561Skato sti # Enable interrupts 31661064Snyan testb $0x1,btx_hdr+0x7 # Reboot? 31743561Skatoexit.3: jz exit.3 # No 318125780Snyan#ifdef PC98 31943561Skato movb $0xa0,%al 32043561Skato outb %al,$0x35 32143561Skato movb 0,%al 32243561Skato outb %al,$0xf0 32343561Skatoexit.4: jmp exit.4 324125780Snyan#else 32558871Skato movw $0x1234, BDA_BOOT # Do a warm boot 326125127Snyan ljmp $0xf000,$0xfff0 # reboot the machine 327125780Snyan#endif 328128710Sru/* 329128710Sru * Set IRQ offsets by reprogramming 8259A PICs. 330128710Sru */ 331125780Snyan#ifdef PC98 33261064Snyansetpic: in $0x02,%al # Save master 33361064Snyan push %ax # IMR 33461064Snyan in $0x0a,%al # Save slave 33561064Snyan push %ax # IMR 33643561Skato movb $0x11,%al # ICW1 to 33743561Skato outb %al,$0x00 # master, 33843561Skato outb %al,$0x08 # slave 33943561Skato movb %bl,%al # ICW2 to 34043561Skato outb %al,$0x02 # master 34143561Skato movb %bh,%al # ICW2 to 34243561Skato outb %al,$0x0a # slave 34343561Skato movb $0x80,%al # ICW3 to 34443561Skato outb %al,$0x02 # master 34543561Skato movb $0x7,%al # ICW3 to 34643561Skato outb %al,$0x0a # slave 34743561Skato movb $0x1d,%al # ICW4 to 34843561Skato outb %al,$0x02 # master, 34943561Skato movb $0x9,%al # ICW4 to 35043561Skato outb %al,$0x0a # slave 35161064Snyan pop %ax # Restore slave 35243561Skato outb %al,$0x0a # IMR 35361064Snyan pop %ax # Restore master 35443561Skato outb %al,$0x02 # IMR 355125780Snyan#else 35661064Snyansetpic: in $0x21,%al # Save master 35761064Snyan push %ax # IMR 35861064Snyan in $0xa1,%al # Save slave 35961064Snyan push %ax # IMR 36043561Skato movb $0x11,%al # ICW1 to 36143561Skato outb %al,$0x20 # master, 36243561Skato outb %al,$0xa0 # slave 36343561Skato movb %bl,%al # ICW2 to 36443561Skato outb %al,$0x21 # master 36543561Skato movb %bh,%al # ICW2 to 36643561Skato outb %al,$0xa1 # slave 36743561Skato movb $0x4,%al # ICW3 to 36843561Skato outb %al,$0x21 # master 36943561Skato movb $0x2,%al # ICW3 to 37043561Skato outb %al,$0xa1 # slave 37143561Skato movb $0x1,%al # ICW4 to 37243561Skato outb %al,$0x21 # master, 37343561Skato outb %al,$0xa1 # slave 37461064Snyan pop %ax # Restore slave 37543561Skato outb %al,$0xa1 # IMR 37661064Snyan pop %ax # Restore master 37743561Skato outb %al,$0x21 # IMR 378125780Snyan#endif 37961064Snyan retw # To caller 38061064Snyan .code32 381128710Sru/* 382128710Sru * Initiate return from V86 mode to user mode. 383128710Sru */ 38443561Skatointhlt: hlt # To supervisor mode 385128710Sru/* 386128710Sru * Exception jump table. 387128710Sru */ 38861064Snyanintx00: push $0x0 # Int 0x0: #DE 38943561Skato jmp ex_noc # Divide error 39061064Snyan push $0x1 # Int 0x1: #DB 39143561Skato jmp ex_noc # Debug 39261064Snyan push $0x3 # Int 0x3: #BP 39343561Skato jmp ex_noc # Breakpoint 39461064Snyan push $0x4 # Int 0x4: #OF 39543561Skato jmp ex_noc # Overflow 39661064Snyan push $0x5 # Int 0x5: #BR 39743561Skato jmp ex_noc # BOUND range exceeded 39861064Snyan push $0x6 # Int 0x6: #UD 39943561Skato jmp ex_noc # Invalid opcode 40061064Snyan push $0x7 # Int 0x7: #NM 40143561Skato jmp ex_noc # Device not available 40261064Snyan push $0x8 # Int 0x8: #DF 40343561Skato jmp except # Double fault 40461064Snyan push $0xa # Int 0xa: #TS 40543561Skato jmp except # Invalid TSS 40661064Snyan push $0xb # Int 0xb: #NP 40743561Skato jmp except # Segment not present 40861064Snyan push $0xc # Int 0xc: #SS 40943561Skato jmp except # Stack segment fault 41061064Snyan push $0xd # Int 0xd: #GP 41143561Skato jmp ex_v86 # General protection 41261064Snyan push $0xe # Int 0xe: #PF 41343561Skato jmp except # Page fault 41461064Snyanintx10: push $0x10 # Int 0x10: #MF 41543561Skato jmp ex_noc # Floating-point error 416128710Sru/* 417128710Sru * Handle #GP exception. 418128710Sru */ 41943561Skatoex_v86: testb $0x2,0x12(%esp,1) # V86 mode? 42043561Skato jz except # No 42143561Skato jmp v86mon # To monitor 422128710Sru/* 423128710Sru * Save a zero error code. 424128710Sru */ 42543561Skatoex_noc: pushl (%esp,1) # Duplicate int no 42643561Skato movb $0x0,0x4(%esp,1) # Fake error code 427128710Sru/* 428128710Sru * Handle exception. 429128710Sru */ 43043561Skatoexcept: cld # String ops inc 43143561Skato pushl %ds # Save 43243561Skato pushl %es # most 43343561Skato pusha # registers 43443561Skato movb $0x6,%al # Push loop count 43543561Skato testb $0x2,0x3a(%esp,1) # V86 mode? 43643561Skato jnz except.1 # Yes 43743561Skato pushl %gs # Set GS 43843561Skato pushl %fs # Set FS 43943561Skato pushl %ds # Set DS 44043561Skato pushl %es # Set ES 44143561Skato movb $0x2,%al # Push loop count 44243561Skato cmpw $SEL_SCODE,0x44(%esp,1) # Supervisor mode? 44343561Skato jne except.1 # No 44443561Skato pushl %ss # Set SS 44543561Skato leal 0x50(%esp,1),%eax # Set 44643561Skato pushl %eax # ESP 44743561Skato jmp except.2 # Join common code 44843561Skatoexcept.1: pushl 0x50(%esp,1) # Set GS, FS, DS, ES 44943561Skato decb %al # (if V86 mode), and 45043561Skato jne except.1 # SS, ESP 45161064Snyanexcept.2: push $SEL_SDATA # Set up 45243561Skato popl %ds # to 45343561Skato pushl %ds # address 45443561Skato popl %es # data 45543561Skato movl %esp,%ebx # Stack frame 45643561Skato movl $dmpfmt,%esi # Dump format string 45743561Skato movl $MEM_BUF,%edi # Buffer 458125780Snyan#ifdef PC98 45943561Skato pushl %eax 46043561Skato pushl %edx 46143561Skatowait.1: 46243561Skato inb $0x60,%al 46343561Skato testb $0x04,%al 46443561Skato jz wait.1 46543561Skato movb $0xe0,%al 46643561Skato outb %al,$0x62 46743561Skatowait.2: 46843561Skato inb $0x60,%al 46943561Skato testb $0x01,%al 47043561Skato jz wait.2 47143561Skato xorl %edx,%edx 47243561Skato inb $0x62,%al 47343561Skato movb %al,%dl 47443561Skato inb $0x62,%al 47543561Skato movb %al,%dh 47643561Skato inb $0x62,%al 47743561Skato inb $0x62,%al 47843561Skato inb $0x62,%al 47943561Skato movl %edx,%eax 48043561Skato shlw $1,%ax 48143561Skato movl $BDA_POS,%edx 48243561Skato movw %ax,(%edx) 48343561Skato popl %edx 48443561Skato popl %eax 485125780Snyan#endif 48643561Skato pushl %edi # Dump to 48743561Skato call dump # buffer 48843561Skato popl %esi # and 48943561Skato call putstr # display 49043561Skato leal 0x18(%esp,1),%esp # Discard frame 49143561Skato popa # Restore 49243561Skato popl %es # registers 49343561Skato popl %ds # saved 49443561Skato cmpb $0x3,(%esp,1) # Breakpoint? 49543561Skato je except.3 # Yes 49686497Snyan cmpb $0x1,(%esp,1) # Debug? 49786497Snyan jne except.2a # No 49886497Snyan testl $0x100,0x10(%esp,1) # Trap flag set? 49986497Snyan jnz except.3 # Yes 50086497Snyanexcept.2a: jmp exit # Exit 50143561Skatoexcept.3: leal 0x8(%esp,1),%esp # Discard err, int no 50243561Skato iret # From interrupt 503128710Sru/* 504128710Sru * Return to user mode from V86 mode. 505128710Sru */ 50643561Skatointrtn: cld # String ops inc 50743561Skato pushl %ds # Address 50843561Skato popl %es # data 50943561Skato leal 0x3c(%ebp),%edx # V86 Segment registers 51043561Skato movl MEM_TSS+TSS_ESP1,%esi # Link stack pointer 51143561Skato lodsl # INT_V86 args pointer 51243561Skato movl %esi,%ebx # Saved exception frame 51343561Skato testl %eax,%eax # INT_V86 args? 51443561Skato jz intrtn.2 # No 51543561Skato movl $MEM_USR,%edi # User base 51643561Skato movl 0x1c(%esi),%ebx # User ESP 51743561Skato movl %eax,(%edi,%ebx,1) # Restore to user stack 51843561Skato leal 0x8(%edi,%eax,1),%edi # Arg segment registers 51943561Skato testb $0x4,-0x6(%edi) # Return flags? 52043561Skato jz intrtn.1 # No 52143561Skato movl 0x30(%ebp),%eax # Get V86 flags 52243561Skato movw %ax,0x18(%esi) # Set user flags 52343561Skatointrtn.1: leal 0x10(%esi),%ebx # Saved exception frame 52443561Skato xchgl %edx,%esi # Segment registers 52543561Skato movb $0x4,%cl # Update seg regs 52643561Skato rep # in INT_V86 52743561Skato movsl # args 52843561Skatointrtn.2: movl %edx,%esi # Segment registers 52943561Skato leal 0x28(%ebp),%edi # Set up seg 53043561Skato movb $0x4,%cl # regs for 53143561Skato rep # later 53243561Skato movsl # pop 53343561Skato movl %ebx,%esi # Restore exception 53443561Skato movb $0x5,%cl # frame to 53543561Skato rep # supervisor 53643561Skato movsl # stack 53743561Skato movl %esi,MEM_TSS+TSS_ESP1 # Link stack pointer 53843561Skato popa # Restore 53943561Skato leal 0x8(%esp,1),%esp # Discard err, int no 54043561Skato popl %es # Restore 54143561Skato popl %ds # user 54243561Skato popl %fs # segment 54343561Skato popl %gs # registers 54443561Skato iret # To user mode 545128710Sru/* 546128710Sru * V86 monitor. 547128710Sru */ 54843561Skatov86mon: cld # String ops inc 54943561Skato pushl $SEL_SDATA # Set up for 55043561Skato popl %ds # flat addressing 55143561Skato pusha # Save registers 55243561Skato movl %esp,%ebp # Address stack frame 55343561Skato movzwl 0x2c(%ebp),%edi # Load V86 CS 55443561Skato shll $0x4,%edi # To linear 55543561Skato movl 0x28(%ebp),%esi # Load V86 IP 55643561Skato addl %edi,%esi # Code pointer 55743561Skato xorl %ecx,%ecx # Zero 55843561Skato movb $0x2,%cl # 16-bit operands 55943561Skato xorl %eax,%eax # Zero 56043561Skatov86mon.1: lodsb # Get opcode 56143561Skato cmpb $0x66,%al # Operand size prefix? 56243561Skato jne v86mon.2 # No 56343561Skato movb $0x4,%cl # 32-bit operands 56443561Skato jmp v86mon.1 # Continue 56543561Skatov86mon.2: cmpb $0xf4,%al # HLT? 56643561Skato jne v86mon.3 # No 56743561Skato cmpl $inthlt+0x1,%esi # Is inthlt? 56844368Skato jne v86mon.7 # No (ignore) 56943561Skato jmp intrtn # Return to user mode 57064019Snyanv86mon.3: cmpb $0xf,%al # Prefixed instruction? 57164019Snyan jne v86mon.4 # No 57264019Snyan cmpb $0x09,(%esi) # Is it a WBINVD? 57364019Snyan je v86wbinvd # Yes 57464019Snyan cmpb $0x30,(%esi) # Is it a WRMSR? 57564019Snyan je v86wrmsr # Yes 57664019Snyan cmpb $0x32,(%esi) # Is it a RDMSR? 57764019Snyan je v86rdmsr # Yes 57864019Snyan cmpb $0x20,(%esi) # Is this a 57944368Skato jne v86mon.4 # MOV EAX,CR0 58044368Skato cmpb $0xc0,0x1(%esi) # instruction? 58144368Skato je v86mov # Yes 58244368Skatov86mon.4: cmpb $0xfa,%al # CLI? 58343561Skato je v86cli # Yes 58443561Skato cmpb $0xfb,%al # STI? 58543561Skato je v86sti # Yes 58643561Skato movzwl 0x38(%ebp),%ebx # Load V86 SS 58743561Skato shll $0x4,%ebx # To offset 58843561Skato pushl %ebx # Save 58943561Skato addl 0x34(%ebp),%ebx # Add V86 SP 59043561Skato movl 0x30(%ebp),%edx # Load V86 flags 59143561Skato cmpb $0x9c,%al # PUSHF/PUSHFD? 59243561Skato je v86pushf # Yes 59343561Skato cmpb $0x9d,%al # POPF/POPFD? 59443561Skato je v86popf # Yes 59543561Skato cmpb $0xcd,%al # INT imm8? 59643561Skato je v86intn # Yes 59743561Skato cmpb $0xcf,%al # IRET/IRETD? 59843561Skato je v86iret # Yes 59943561Skato popl %ebx # Restore 60043561Skato popa # Restore 60143561Skato jmp except # Handle exception 60244368Skatov86mon.5: movl %edx,0x30(%ebp) # Save V86 flags 60344368Skatov86mon.6: popl %edx # V86 SS adjustment 60443561Skato subl %edx,%ebx # Save V86 60543561Skato movl %ebx,0x34(%ebp) # SP 60644368Skatov86mon.7: subl %edi,%esi # From linear 60743561Skato movl %esi,0x28(%ebp) # Save V86 IP 60843561Skato popa # Restore 60943561Skato leal 0x8(%esp,1),%esp # Discard int no, error 61043561Skato iret # To V86 mode 611128710Sru/* 612128710Sru * Emulate MOV EAX,CR0. 613128710Sru */ 61444368Skatov86mov: movl %cr0,%eax # CR0 to 61544368Skato movl %eax,0x1c(%ebp) # saved EAX 61664019Snyan incl %esi # Adjust IP 617128710Sru/* 618128710Sru * Return from emulating a 0x0f prefixed instruction 619128710Sru */ 62064019Snyanv86preret: incl %esi # Adjust IP 62144368Skato jmp v86mon.7 # Finish up 622128710Sru/* 623128710Sru * Emulate WBINVD 624128710Sru */ 62564019Snyanv86wbinvd: wbinvd # Write back and invalidate 62664019Snyan # cache 62764019Snyan jmp v86preret # Finish up 628128710Sru/* 629128710Sru * Emulate WRMSR 630128710Sru */ 63164019Snyanv86wrmsr: movl 0x18(%ebp),%ecx # Get user's %ecx (MSR to write) 63264019Snyan movl 0x14(%ebp),%edx # Load the value 63364019Snyan movl 0x1c(%ebp),%eax # to write 63464019Snyan wrmsr # Write MSR 63564019Snyan jmp v86preret # Finish up 636128710Sru/* 637128710Sru * Emulate RDMSR 638128710Sru */ 63964019Snyanv86rdmsr: movl 0x18(%ebp),%ecx # MSR to read 64064019Snyan rdmsr # Read the MSR 64164019Snyan movl %eax,0x1c(%ebp) # Return the value of 64264019Snyan movl %edx,0x14(%ebp) # the MSR to the user 64364019Snyan jmp v86preret # Finish up 644128710Sru/* 645128710Sru * Emulate CLI. 646128710Sru */ 64743561Skatov86cli: andb $~0x2,0x31(%ebp) # Clear IF 64844368Skato jmp v86mon.7 # Finish up 649128710Sru/* 650128710Sru * Emulate STI. 651128710Sru */ 65243561Skatov86sti: orb $0x2,0x31(%ebp) # Set IF 65344368Skato jmp v86mon.7 # Finish up 654128710Sru/* 655128710Sru * Emulate PUSHF/PUSHFD. 656128710Sru */ 65743561Skatov86pushf: subl %ecx,%ebx # Adjust SP 65843561Skato cmpb $0x4,%cl # 32-bit 65943561Skato je v86pushf.1 # Yes 66061064Snyan data16 # 16-bit 66143561Skatov86pushf.1: movl %edx,(%ebx) # Save flags 66244368Skato jmp v86mon.6 # Finish up 663128710Sru/* 664128710Sru * Emulate IRET/IRETD. 665128710Sru */ 66643561Skatov86iret: movzwl (%ebx),%esi # Load V86 IP 66743561Skato movzwl 0x2(%ebx),%edi # Load V86 CS 66843561Skato leal 0x4(%ebx),%ebx # Adjust SP 66943561Skato movl %edi,0x2c(%ebp) # Save V86 CS 67043561Skato xorl %edi,%edi # No ESI adjustment 671128710Sru/* 672128710Sru * Emulate POPF/POPFD (and remainder of IRET/IRETD). 673128710Sru */ 67443561Skatov86popf: cmpb $0x4,%cl # 32-bit? 67543561Skato je v86popf.1 # Yes 67643561Skato movl %edx,%eax # Initialize 67761064Snyan data16 # 16-bit 67843561Skatov86popf.1: movl (%ebx),%eax # Load flags 67943561Skato addl %ecx,%ebx # Adjust SP 68043561Skato andl $V86_FLG,%eax # Merge 68143561Skato andl $~V86_FLG,%edx # the 68243561Skato orl %eax,%edx # flags 68344368Skato jmp v86mon.5 # Finish up 684128710Sru/* 685128710Sru * trap int 15, function 87 686128710Sru * reads %es:%si from saved registers on stack to find a GDT containing 687128710Sru * source and destination locations 688128710Sru * reads count of words from saved %cx 689128710Sru * returns success by setting %ah to 0 690128710Sru */ 691128710Sruint15_87: pushl %eax # Save 692128710Sru pushl %ebx # some information 69352202Skato pushl %esi # onto the stack. 69452202Skato pushl %edi 695128710Sru xorl %eax,%eax # clean EAX 696128710Sru xorl %ebx,%ebx # clean EBX 69752202Skato movl 0x4(%ebp),%esi # Get user's ESI 69852202Skato movl 0x3C(%ebp),%ebx # store ES 69952202Skato movw %si,%ax # store SI 70052202Skato shll $0x4,%ebx # Make it a seg. 70152202Skato addl %eax,%ebx # ebx=(es<<4)+si 70252202Skato movb 0x14(%ebx),%al # Grab the 70352202Skato movb 0x17(%ebx),%ah # necessary 70452202Skato shll $0x10,%eax # information 70552202Skato movw 0x12(%ebx),%ax # from 70652202Skato movl %eax,%esi # the 70752202Skato movb 0x1c(%ebx),%al # GDT in order to 70852202Skato movb 0x1f(%ebx),%ah # have %esi offset 70952202Skato shll $0x10,%eax # of source and %edi 71052202Skato movw 0x1a(%ebx),%ax # of destination. 71152202Skato movl %eax,%edi 71252202Skato pushl %ds # Make: 71352202Skato popl %es # es = ds 71452202Skato pushl %ecx # stash ECX 71552202Skato xorl %ecx,%ecx # highw of ECX is clear 71652202Skato movw 0x18(%ebp),%cx # Get user's ECX 71758871Skato shll $0x1,%ecx # Convert from num words to num 71858871Skato # bytes 71952202Skato rep # repeat... 72052202Skato movsb # perform copy. 72152202Skato popl %ecx # Restore 72252202Skato popl %edi 72352202Skato popl %esi # previous 72452202Skato popl %ebx # register 72552202Skato popl %eax # values. 72652202Skato movb $0x0,0x1d(%ebp) # set ah = 0 to indicate 72752202Skato # success 72852202Skato andb $0xfe,%dl # clear CF 72952202Skato jmp v86mon.5 # Finish up 73052202Skato 731128710Sru/* 732128710Sru * Reboot the machine by setting the reboot flag and exiting 733128710Sru */ 73458871Skatoreboot: orb $0x1,btx_hdr+0x7 # Set the reboot flag 73558871Skato jmp exit # Terminate BTX and reboot 73658871Skato 737128710Sru/* 738128710Sru * Emulate INT imm8... also make sure to check if it's int 15/87 739128710Sru */ 74043561Skatov86intn: lodsb # Get int no 74158871Skato cmpb $0x19,%al # is it int 19? 74258871Skato je reboot # yes, reboot the machine 74352202Skato cmpb $0x15,%al # is it int 15? 74458871Skato jne v86intn.3 # no, skip parse 74552202Skato pushl %eax # stash EAX 74652202Skato movl 0x1c(%ebp),%eax # user's saved EAX 74758871Skato cmpb $0x87,%ah # is it the memcpy subfunction? 74858871Skato jne v86intn.1 # no, keep checking 74952202Skato popl %eax # get the stack straight 75052202Skato jmp int15_87 # it's our cue 75158871Skatov86intn.1: cmpw $0x4f53,%ax # is it the delete key callout? 75258871Skato jne v86intn.2 # no, handle the int normally 75358871Skato movb BDA_KEYFLAGS,%al # get the shift key state 754125780Snyan#ifdef PC98 75558871Skato andb $0x18,%al # mask off just Ctrl and Alt 75658871Skato cmpb $0x18,%al # are both Ctrl and Alt down? 757125780Snyan#else 75858871Skato andb $0xc,%al # mask off just Ctrl and Alt 75958871Skato cmpb $0xc,%al # are both Ctrl and Alt down? 760125780Snyan#endif 76158871Skato jne v86intn.2 # no, handle the int normally 76258871Skato popl %eax # restore EAX 76358871Skato jmp reboot # reboot the machine 76458871Skatov86intn.2: popl %eax # restore EAX 76558871Skatov86intn.3: subl %edi,%esi # From 76643561Skato shrl $0x4,%edi # linear 76743561Skato movw %dx,-0x2(%ebx) # Save flags 76843561Skato movw %di,-0x4(%ebx) # Save CS 76943561Skato leal -0x6(%ebx),%ebx # Adjust SP 77043561Skato movw %si,(%ebx) # Save IP 77143561Skato shll $0x2,%eax # Scale 77243561Skato movzwl (%eax),%esi # Load IP 77343561Skato movzwl 0x2(%eax),%edi # Load CS 77443561Skato movl %edi,0x2c(%ebp) # Save CS 77543561Skato xorl %edi,%edi # No ESI adjustment 77661064Snyan andb $~0x1,%dh # Clear TF 77744368Skato jmp v86mon.5 # Finish up 778128710Sru/* 779128710Sru * Hardware interrupt jump table. 780128710Sru */ 78161064Snyanintx20: push $0x8 # Int 0x20: IRQ0 78243561Skato jmp int_hw # V86 int 0x8 78361064Snyan push $0x9 # Int 0x21: IRQ1 78443561Skato jmp int_hw # V86 int 0x9 78561064Snyan push $0xa # Int 0x22: IRQ2 78643561Skato jmp int_hw # V86 int 0xa 78761064Snyan push $0xb # Int 0x23: IRQ3 78843561Skato jmp int_hw # V86 int 0xb 78961064Snyan push $0xc # Int 0x24: IRQ4 79043561Skato jmp int_hw # V86 int 0xc 79161064Snyan push $0xd # Int 0x25: IRQ5 79243561Skato jmp int_hw # V86 int 0xd 79361064Snyan push $0xe # Int 0x26: IRQ6 79443561Skato jmp int_hw # V86 int 0xe 79561064Snyan push $0xf # Int 0x27: IRQ7 79643561Skato jmp int_hw # V86 int 0xf 797125780Snyan#ifdef PC98 79861064Snyan push $0x10 # Int 0x28: IRQ8 79943561Skato jmp int_hw # V86 int 0x10 80061064Snyan push $0x11 # Int 0x29: IRQ9 80143561Skato jmp int_hw # V86 int 0x11 80261064Snyan push $0x12 # Int 0x2a: IRQ10 80343561Skato jmp int_hw # V86 int 0x12 80461064Snyan push $0x13 # Int 0x2b: IRQ11 80543561Skato jmp int_hw # V86 int 0x13 80661064Snyan push $0x14 # Int 0x2c: IRQ12 80743561Skato jmp int_hw # V86 int 0x14 80861064Snyan push $0x15 # Int 0x2d: IRQ13 80943561Skato jmp int_hw # V86 int 0x15 81061064Snyan push $0x16 # Int 0x2e: IRQ14 81143561Skato jmp int_hw # V86 int 0x16 81261064Snyan push $0x17 # Int 0x2f: IRQ15 81343561Skato jmp int_hw # V86 int 0x17 814125780Snyan#else 81561064Snyan push $0x70 # Int 0x28: IRQ8 81643561Skato jmp int_hw # V86 int 0x70 81761064Snyan push $0x71 # Int 0x29: IRQ9 81843561Skato jmp int_hw # V86 int 0x71 81961064Snyan push $0x72 # Int 0x2a: IRQ10 82043561Skato jmp int_hw # V86 int 0x72 82161064Snyan push $0x73 # Int 0x2b: IRQ11 82243561Skato jmp int_hw # V86 int 0x73 82361064Snyan push $0x74 # Int 0x2c: IRQ12 82443561Skato jmp int_hw # V86 int 0x74 82561064Snyan push $0x75 # Int 0x2d: IRQ13 82643561Skato jmp int_hw # V86 int 0x75 82761064Snyan push $0x76 # Int 0x2e: IRQ14 82843561Skato jmp int_hw # V86 int 0x76 82961064Snyan push $0x77 # Int 0x2f: IRQ15 83043561Skato jmp int_hw # V86 int 0x77 831125780Snyan#endif 832128710Sru/* 833128710Sru * Reflect hardware interrupts. 834128710Sru */ 83543561Skatoint_hw: testb $0x2,0xe(%esp,1) # V86 mode? 83643561Skato jz intusr # No 83743561Skato pushl $SEL_SDATA # Address 83843561Skato popl %ds # data 83943561Skato xchgl %eax,(%esp,1) # Swap EAX, int no 84043561Skato pushl %ebp # Address 84143561Skato movl %esp,%ebp # stack frame 84243561Skato pushl %ebx # Save 84343561Skato shll $0x2,%eax # Get int 84443561Skato movl (%eax),%eax # vector 84543561Skato subl $0x6,0x14(%ebp) # Adjust V86 ESP 84643561Skato movzwl 0x18(%ebp),%ebx # V86 SS 84743561Skato shll $0x4,%ebx # * 0x10 84843561Skato addl 0x14(%ebp),%ebx # + V86 ESP 84943561Skato xchgw %ax,0x8(%ebp) # Swap V86 IP 85043561Skato rorl $0x10,%eax # Swap words 85143561Skato xchgw %ax,0xc(%ebp) # Swap V86 CS 85243561Skato roll $0x10,%eax # Swap words 85343561Skato movl %eax,(%ebx) # CS:IP for IRET 85443561Skato movl 0x10(%ebp),%eax # V86 flags 85543561Skato movw %ax,0x4(%ebx) # Flags for IRET 85643561Skato andb $~0x3,0x11(%ebp) # Clear IF, TF 85743561Skato popl %ebx # Restore 85843561Skato popl %ebp # saved 85943561Skato popl %eax # registers 86043561Skato iret # To V86 mode 861128710Sru/* 862128710Sru * Invoke V86 interrupt from user mode, with arguments. 863128710Sru */ 86443561Skatointx31: stc # Have btx_v86 86543561Skato pushl %eax # Missing int no 866128710Sru/* 867128710Sru * Invoke V86 interrupt from user mode. 868128710Sru */ 86943561Skatointusr: std # String ops dec 87043561Skato pushl %eax # Expand 87143561Skato pushl %eax # stack 87243561Skato pushl %eax # frame 87343561Skato pusha # Save 87443561Skato pushl %gs # Save 87543561Skato movl %esp,%eax # seg regs 87643561Skato pushl %fs # and 87743561Skato pushl %ds # point 87843561Skato pushl %es # to them 87961064Snyan push $SEL_SDATA # Set up 88043561Skato popl %ds # to 88143561Skato pushl %ds # address 88243561Skato popl %es # data 88343561Skato movl $MEM_USR,%ebx # User base 88443561Skato movl %ebx,%edx # address 88543561Skato jc intusr.1 # If btx_v86 88643561Skato xorl %edx,%edx # Control flags 88743561Skato xorl %ebp,%ebp # btx_v86 pointer 88843561Skatointusr.1: leal 0x50(%esp,1),%esi # Base of frame 88943561Skato pushl %esi # Save 89043561Skato addl -0x4(%esi),%ebx # User ESP 89143561Skato movl MEM_TSS+TSS_ESP1,%edi # Link stack pointer 89243561Skato leal -0x4(%edi),%edi # Adjust for push 89343561Skato xorl %ecx,%ecx # Zero 89443561Skato movb $0x5,%cl # Push exception 89543561Skato rep # frame on 89643561Skato movsl # link stack 89743561Skato xchgl %eax,%esi # Saved seg regs 89843561Skato movl 0x40(%esp,1),%eax # Get int no 89943561Skato testl %edx,%edx # Have btx_v86? 90043561Skato jz intusr.2 # No 90143561Skato movl (%ebx),%ebp # btx_v86 pointer 90243561Skato movb $0x4,%cl # Count 90343561Skato addl %ecx,%ebx # Adjust for pop 90443561Skato rep # Push saved seg regs 90543561Skato movsl # on link stack 90643561Skato addl %ebp,%edx # Flatten btx_v86 ptr 90743561Skato leal 0x14(%edx),%esi # Seg regs pointer 90843561Skato movl 0x4(%edx),%eax # Get int no/address 90943561Skato movzwl 0x2(%edx),%edx # Get control flags 91043561Skatointusr.2: movl %ebp,(%edi) # Push btx_v86 and 91143561Skato movl %edi,MEM_TSS+TSS_ESP1 # save link stack ptr 91243561Skato popl %edi # Base of frame 91343561Skato xchgl %eax,%ebp # Save intno/address 91443561Skato movl 0x48(%esp,1),%eax # Get flags 91543561Skato testb $0x2,%dl # Simulate CALLF? 91643561Skato jnz intusr.3 # Yes 91743561Skato decl %ebx # Push flags 91843561Skato decl %ebx # on V86 91943561Skato movw %ax,(%ebx) # stack 92043561Skatointusr.3: movb $0x4,%cl # Count 92143561Skato subl %ecx,%ebx # Push return address 92243561Skato movl $inthlt,(%ebx) # on V86 stack 92343561Skato rep # Copy seg regs to 92443561Skato movsl # exception frame 92543561Skato xchgl %eax,%ecx # Save flags 92643561Skato movl %ebx,%eax # User ESP 92743561Skato subl $V86_STK,%eax # Less bytes 92843561Skato ja intusr.4 # to 92943561Skato xorl %eax,%eax # keep 93043561Skatointusr.4: shrl $0x4,%eax # Gives segment 93143561Skato stosl # Set SS 93243561Skato shll $0x4,%eax # To bytes 93343561Skato xchgl %eax,%ebx # Swap 93443561Skato subl %ebx,%eax # Gives offset 93543561Skato stosl # Set ESP 93643561Skato xchgl %eax,%ecx # Get flags 93743561Skato btsl $0x11,%eax # Set VM 93861064Snyan andb $~0x1,%ah # Clear TF 93943561Skato stosl # Set EFL 94043561Skato xchgl %eax,%ebp # Get int no/address 94143561Skato testb $0x1,%dl # Address? 94243561Skato jnz intusr.5 # Yes 94343561Skato shll $0x2,%eax # Scale 94443561Skato movl (%eax),%eax # Load int vector 94543561Skatointusr.5: movl %eax,%ecx # Save 94643561Skato shrl $0x10,%eax # Gives segment 94743561Skato stosl # Set CS 94843561Skato movw %cx,%ax # Restore 94943561Skato stosl # Set EIP 95043561Skato leal 0x10(%esp,1),%esp # Discard seg regs 95143561Skato popa # Restore 95243561Skato iret # To V86 mode 953128710Sru/* 954128710Sru * System Call. 955128710Sru */ 95643561Skatointx30: cmpl $SYS_EXEC,%eax # Exec system call? 95743561Skato jne intx30.1 # No 95843561Skato pushl %ss # Set up 95943561Skato popl %es # all 96043561Skato pushl %es # segment 96143561Skato popl %ds # registers 96243561Skato pushl %ds # for the 96343561Skato popl %fs # program 96443561Skato pushl %fs # we're 96543561Skato popl %gs # invoking 96643561Skato movl $MEM_USR,%eax # User base address 96743561Skato addl 0xc(%esp,1),%eax # Change to user 96843561Skato leal 0x4(%eax),%esp # stack 969125780Snyan#ifdef PAGING 97043561Skato movl %cr0,%eax # Turn 97143561Skato andl $~0x80000000,%eax # off 97243561Skato movl %eax,%cr0 # paging 97343561Skato xorl %eax,%eax # Flush 97443561Skato movl %eax,%cr3 # TLB 975125780Snyan#endif 97643561Skato popl %eax # Call 97743561Skato call *%eax # program 97878650Skatointx30.1: orb $0x1,%ss:btx_hdr+0x7 # Flag reboot 97943561Skato jmp exit # Exit 980128710Sru/* 981128710Sru * Dump structure [EBX] to [EDI], using format string [ESI]. 982128710Sru */ 98343561Skatodump.0: stosb # Save char 98443561Skatodump: lodsb # Load char 98543561Skato testb %al,%al # End of string? 98643561Skato jz dump.10 # Yes 98743561Skato testb $0x80,%al # Control? 98843561Skato jz dump.0 # No 98943561Skato movb %al,%ch # Save control 99043561Skato movb $'=',%al # Append 99143561Skato stosb # '=' 99243561Skato lodsb # Get offset 99343561Skato pushl %esi # Save 99443561Skato movsbl %al,%esi # To 99543561Skato addl %ebx,%esi # pointer 99643561Skato testb $DMP_X16,%ch # Dump word? 99743561Skato jz dump.1 # No 99843561Skato lodsw # Get and 99943561Skato call hex16 # dump it 100043561Skatodump.1: testb $DMP_X32,%ch # Dump long? 100143561Skato jz dump.2 # No 100243561Skato lodsl # Get and 100343561Skato call hex32 # dump it 100443561Skatodump.2: testb $DMP_MEM,%ch # Dump memory? 100543561Skato jz dump.8 # No 100643561Skato pushl %ds # Save 100743561Skato testb $0x2,0x52(%ebx) # V86 mode? 100843561Skato jnz dump.3 # Yes 100961064Snyan verr 0x4(%esi) # Readable selector? 101043561Skato jnz dump.3 # No 101143561Skato ldsl (%esi),%esi # Load pointer 101243561Skato jmp dump.4 # Join common code 101343561Skatodump.3: lodsl # Set offset 101443561Skato xchgl %eax,%edx # Save 101543561Skato lodsl # Get segment 101643561Skato shll $0x4,%eax # * 0x10 101743561Skato addl %edx,%eax # + offset 101843561Skato xchgl %eax,%esi # Set pointer 101986497Snyandump.4: movb $2,%dl # Num lines 102086497Snyandump.4a: movb $0x10,%cl # Bytes to dump 102143561Skatodump.5: lodsb # Get byte and 102243561Skato call hex8 # dump it 102343561Skato decb %cl # Keep count 102486497Snyan jz dump.6a # If done 102543561Skato movb $'-',%al # Separator 102643561Skato cmpb $0x8,%cl # Half way? 102743561Skato je dump.6 # Yes 102843561Skato movb $' ',%al # Use space 102943561Skatodump.6: stosb # Save separator 103043561Skato jmp dump.5 # Continue 103186497Snyandump.6a: decb %dl # Keep count 103286497Snyan jz dump.7 # If done 103386497Snyan movb $0xa,%al # Line feed 103486497Snyan stosb # Save one 103586497Snyan movb $7,%cl # Leading 103686497Snyan movb $' ',%al # spaces 103786497Snyandump.6b: stosb # Dump 103886497Snyan decb %cl # spaces 103986497Snyan jnz dump.6b 104086497Snyan jmp dump.4a # Next line 104143561Skatodump.7: popl %ds # Restore 104243561Skatodump.8: popl %esi # Restore 104343561Skato movb $0xa,%al # Line feed 104443561Skato testb $DMP_EOL,%ch # End of line? 104543561Skato jnz dump.9 # Yes 104643561Skato movb $' ',%al # Use spaces 104743561Skato stosb # Save one 104843561Skatodump.9: jmp dump.0 # Continue 104943561Skatodump.10: stosb # Terminate string 105043561Skato ret # To caller 1051128710Sru/* 1052128710Sru * Convert EAX, AX, or AL to hex, saving the result to [EDI]. 1053128710Sru */ 105443561Skatohex32: pushl %eax # Save 105543561Skato shrl $0x10,%eax # Do upper 105643561Skato call hex16 # 16 105743561Skato popl %eax # Restore 105843561Skatohex16: call hex16.1 # Do upper 8 105943561Skatohex16.1: xchgb %ah,%al # Save/restore 106043561Skatohex8: pushl %eax # Save 106143561Skato shrb $0x4,%al # Do upper 106243561Skato call hex8.1 # 4 106343561Skato popl %eax # Restore 106443561Skatohex8.1: andb $0xf,%al # Get lower 4 106543561Skato cmpb $0xa,%al # Convert 106643561Skato sbbb $0x69,%al # to hex 106743561Skato das # digit 106843561Skato orb $0x20,%al # To lower case 106943561Skato stosb # Save char 107043561Skato ret # (Recursive) 1071128710Sru/* 1072128710Sru * Output zero-terminated string [ESI] to the console. 1073128710Sru */ 107443561Skatoputstr.0: call putchr # Output char 107543561Skatoputstr: lodsb # Load char 107643561Skato testb %al,%al # End of string? 107743561Skato jnz putstr.0 # No 107843561Skato ret # To caller 1079125780Snyan#ifdef BTX_SERIAL 108086497Snyan .set SIO_PRT,SIOPRT # Base port 108186497Snyan .set SIO_FMT,SIOFMT # 8N1 108286497Snyan .set SIO_DIV,(115200/SIOSPD) # 115200 / SPD 108386497Snyan 1084138189Snyan/* 1085128710Sru * void sio_init(void) 1086138189Snyan */ 108786497Snyansio_init: movw $SIO_PRT+0x3,%dx # Data format reg 108886497Snyan movb $SIO_FMT|0x80,%al # Set format 108986497Snyan outb %al,(%dx) # and DLAB 109086497Snyan pushl %edx # Save 109186497Snyan subb $0x3,%dl # Divisor latch reg 109286497Snyan movw $SIO_DIV,%ax # Set 109386497Snyan outw %ax,(%dx) # BPS 109486497Snyan popl %edx # Restore 109586497Snyan movb $SIO_FMT,%al # Clear 109686497Snyan outb %al,(%dx) # DLAB 109786497Snyan incl %edx # Modem control reg 109886497Snyan movb $0x3,%al # Set RTS, 109986497Snyan outb %al,(%dx) # DTR 110086497Snyan incl %edx # Line status reg 110186497Snyan 1102138189Snyan/* 1103128710Sru * void sio_flush(void) 1104138189Snyan */ 110586497Snyansio_flush.0: call sio_getc.1 # Get character 110686497Snyansio_flush: call sio_ischar # Check for character 110786497Snyan jnz sio_flush.0 # Till none 110886497Snyan ret # To caller 110986497Snyan 1110138189Snyan/* 1111128710Sru * void sio_putc(int c) 1112138189Snyan */ 111386497Snyansio_putc: movw $SIO_PRT+0x5,%dx # Line status reg 111486497Snyan xor %ecx,%ecx # Timeout 111586497Snyan movb $0x40,%ch # counter 111686497Snyansio_putc.1: inb (%dx),%al # Transmitter 111786497Snyan testb $0x20,%al # buffer empty? 111886497Snyan loopz sio_putc.1 # No 111986497Snyan jz sio_putc.2 # If timeout 112086497Snyan movb 0x4(%esp,1),%al # Get character 112186497Snyan subb $0x5,%dl # Transmitter hold reg 112286497Snyan outb %al,(%dx) # Write character 112386497Snyansio_putc.2: ret $0x4 # To caller 112486497Snyan 1125138189Snyan/* 1126128710Sru * int sio_getc(void) 1127138189Snyan */ 112886497Snyansio_getc: call sio_ischar # Character available? 112986497Snyan jz sio_getc # No 113086497Snyansio_getc.1: subb $0x5,%dl # Receiver buffer reg 113186497Snyan inb (%dx),%al # Read character 113286497Snyan ret # To caller 113386497Snyan 1134138189Snyan/* 1135128710Sru * int sio_ischar(void) 1136138189Snyan */ 113786497Snyansio_ischar: movw $SIO_PRT+0x5,%dx # Line status register 113886497Snyan xorl %eax,%eax # Zero 113986497Snyan inb (%dx),%al # Received data 114086497Snyan andb $0x1,%al # ready? 114186497Snyan ret # To caller 114286497Snyan 1143128710Sru/* 1144128710Sru * Output character AL to the serial console. 1145128710Sru */ 114686497Snyanputchr: pusha # Save 114786497Snyan cmpb $10, %al # is it a newline? 114886497Snyan jne putchr.1 # no?, then leave 114986497Snyan push $13 # output a carriage 115086497Snyan call sio_putc # return first 115186497Snyan movb $10, %al # restore %al 115286497Snyanputchr.1: pushl %eax # Push the character 115386497Snyan # onto the stack 115486497Snyan call sio_putc # Output the character 115586497Snyan popa # Restore 115686497Snyan ret # To caller 1157125780Snyan#else 1158128710Sru/* 1159128710Sru * Output character AL to the console. 1160128710Sru */ 116143561Skatoputchr: pusha # Save 116243561Skato xorl %ecx,%ecx # Zero for loops 116343561Skato movb $SCR_MAT,%ah # Mode/attribute 116443561Skato movl $BDA_POS,%ebx # BDA pointer 116543561Skato movw (%ebx),%dx # Cursor position 1166125780Snyan#ifdef PC98 116743561Skato movl $0xa0000,%edi 1168125780Snyan#else 116943561Skato movl $0xb8000,%edi # Regen buffer (color) 117043561Skato cmpb %ah,BDA_SCR-BDA_POS(%ebx) # Mono mode? 117143561Skato jne putchr.1 # No 117243561Skato xorw %di,%di # Regen buffer (mono) 1173125780Snyan#endif 117443561Skatoputchr.1: cmpb $0xa,%al # New line? 117543561Skato je putchr.2 # Yes 1176125780Snyan#ifdef PC98 117743561Skato movw %dx,%cx 117843561Skato movb %al,(%edi,%ecx,1) # Write char 117943561Skato addl $0x2000,%ecx 118043561Skato movb %ah,(%edi,%ecx,1) # Write attr 118143561Skato addw $0x02,%dx 118243561Skato jmp putchr.3 118343561Skatoputchr.2: movw %dx,%ax 118443561Skato movb $SCR_COL*2,%dl 118543561Skato div %dl 118643561Skato incb %al 118743561Skato mul %dl 118843561Skato movw %ax,%dx 118943561Skatoputchr.3: cmpw $SCR_ROW*SCR_COL*2,%dx 1190125780Snyan#else 119143561Skato xchgl %eax,%ecx # Save char 119243561Skato movb $SCR_COL,%al # Columns per row 119343561Skato mulb %dh # * row position 119443561Skato addb %dl,%al # + column 119543561Skato adcb $0x0,%ah # position 119643561Skato shll %eax # * 2 119743561Skato xchgl %eax,%ecx # Swap char, offset 119843561Skato movw %ax,(%edi,%ecx,1) # Write attr:char 119943561Skato incl %edx # Bump cursor 120043561Skato cmpb $SCR_COL,%dl # Beyond row? 120143561Skato jb putchr.3 # No 120243561Skatoputchr.2: xorb %dl,%dl # Zero column 120343561Skato incb %dh # Bump row 120443561Skatoputchr.3: cmpb $SCR_ROW,%dh # Beyond screen? 1205125780Snyan#endif 120643561Skato jb putchr.4 # No 120743561Skato leal 2*SCR_COL(%edi),%esi # New top line 120843561Skato movw $(SCR_ROW-1)*SCR_COL/2,%cx # Words to move 120943561Skato rep # Scroll 121043561Skato movsl # screen 121186497Snyan movb $0x20,%al # Space 1212125780Snyan#ifdef PC98 121343561Skato xorb %ah,%ah 1214125780Snyan#endif 121543561Skato movb $SCR_COL,%cl # Columns to clear 121643561Skato rep # Clear 121743561Skato stosw # line 1218125780Snyan#ifdef PC98 121943561Skato movw $(SCR_ROW-1)*SCR_COL*2,%dx 1220125780Snyan#else 122143561Skato movb $SCR_ROW-1,%dh # Bottom line 1222125780Snyan#endif 122343561Skatoputchr.4: movw %dx,(%ebx) # Update position 122443561Skato popa # Restore 122543561Skato ret # To caller 1226125780Snyan#endif 122743561Skato 122843561Skato .p2align 4 1229128710Sru/* 1230128710Sru * Global descriptor table. 1231128710Sru */ 123243561Skatogdt: .word 0x0,0x0,0x0,0x0 # Null entry 123343561Skato .word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE 123443561Skato .word 0xffff,0x0,0x9200,0xcf # SEL_SDATA 123543561Skato .word 0xffff,0x0,0x9a00,0x0 # SEL_RCODE 123643561Skato .word 0xffff,0x0,0x9200,0x0 # SEL_RDATA 123743561Skato .word 0xffff,MEM_USR,0xfa00,0xcf# SEL_UCODE 123843561Skato .word 0xffff,MEM_USR,0xf200,0xcf# SEL_UDATA 123943561Skato .word _TSSLM,MEM_TSS,0x8900,0x0 # SEL_TSS 124043561Skatogdt.1: 1241128710Sru/* 1242128710Sru * Pseudo-descriptors. 1243128710Sru */ 124443561Skatogdtdesc: .word gdt.1-gdt-1,gdt,0x0 # GDT 124543561Skatoidtdesc: .word _IDTLM,MEM_IDT,0x0 # IDT 124643561Skatoivtdesc: .word 0x400-0x0-1,0x0,0x0 # IVT 1247128710Sru/* 1248128710Sru * IDT construction control string. 1249128710Sru */ 125043561Skatoidtctl: .byte 0x10, 0x8e # Int 0x0-0xf 125143561Skato .word 0x7dfb,intx00 # (exceptions) 125243561Skato .byte 0x10, 0x8e # Int 0x10 125343561Skato .word 0x1, intx10 # (exception) 125443561Skato .byte 0x10, 0x8e # Int 0x20-0x2f 125543561Skato .word 0xffff,intx20 # (hardware) 125643561Skato .byte 0x1, 0xee # int 0x30 125743561Skato .word 0x1, intx30 # (system call) 125843561Skato .byte 0x2, 0xee # Int 0x31-0x32 125943561Skato .word 0x1, intx31 # (V86, null) 126043561Skato .byte 0x0 # End of string 1261128710Sru/* 1262128710Sru * Dump format string. 1263128710Sru */ 126443561Skatodmpfmt: .byte '\n' # "\n" 126543561Skato .ascii "int" # "int=" 126643561Skato .byte 0x80|DMP_X32, 0x40 # "00000000 " 126743561Skato .ascii "err" # "err=" 126843561Skato .byte 0x80|DMP_X32, 0x44 # "00000000 " 126943561Skato .ascii "efl" # "efl=" 127043561Skato .byte 0x80|DMP_X32, 0x50 # "00000000 " 127143561Skato .ascii "eip" # "eip=" 127243561Skato .byte 0x80|DMP_X32|DMP_EOL,0x48 # "00000000\n" 127343561Skato .ascii "eax" # "eax=" 127443561Skato .byte 0x80|DMP_X32, 0x34 # "00000000 " 127543561Skato .ascii "ebx" # "ebx=" 127643561Skato .byte 0x80|DMP_X32, 0x28 # "00000000 " 127743561Skato .ascii "ecx" # "ecx=" 127843561Skato .byte 0x80|DMP_X32, 0x30 # "00000000 " 127943561Skato .ascii "edx" # "edx=" 128043561Skato .byte 0x80|DMP_X32|DMP_EOL,0x2c # "00000000\n" 128143561Skato .ascii "esi" # "esi=" 128243561Skato .byte 0x80|DMP_X32, 0x1c # "00000000 " 128343561Skato .ascii "edi" # "edi=" 128443561Skato .byte 0x80|DMP_X32, 0x18 # "00000000 " 128543561Skato .ascii "ebp" # "ebp=" 128643561Skato .byte 0x80|DMP_X32, 0x20 # "00000000 " 128743561Skato .ascii "esp" # "esp=" 128843561Skato .byte 0x80|DMP_X32|DMP_EOL,0x0 # "00000000\n" 128943561Skato .ascii "cs" # "cs=" 129043561Skato .byte 0x80|DMP_X16, 0x4c # "0000 " 129143561Skato .ascii "ds" # "ds=" 129243561Skato .byte 0x80|DMP_X16, 0xc # "0000 " 129343561Skato .ascii "es" # "es=" 129443561Skato .byte 0x80|DMP_X16, 0x8 # "0000 " 129543561Skato .ascii " " # " " 129643561Skato .ascii "fs" # "fs=" 129743561Skato .byte 0x80|DMP_X16, 0x10 # "0000 " 129843561Skato .ascii "gs" # "gs=" 129943561Skato .byte 0x80|DMP_X16, 0x14 # "0000 " 130043561Skato .ascii "ss" # "ss=" 130143561Skato .byte 0x80|DMP_X16|DMP_EOL,0x4 # "0000\n" 130243561Skato .ascii "cs:eip" # "cs:eip=" 130343561Skato .byte 0x80|DMP_MEM|DMP_EOL,0x48 # "00 00 ... 00 00\n" 130443561Skato .ascii "ss:esp" # "ss:esp=" 130543561Skato .byte 0x80|DMP_MEM|DMP_EOL,0x0 # "00 00 ... 00 00\n" 130686497Snyan .asciz "BTX halted\n" # End 1307128710Sru/* 1308128710Sru * End of BTX memory. 1309128710Sru */ 131043561Skato .p2align 4 131143561Skatobreak: 1312