btx.S revision 52202
143561Skato#
243561Skato# Copyright (c) 1998 Robert Nordier
343561Skato# All rights reserved.
443561Skato#
543561Skato# Redistribution and use in source and binary forms are freely
643561Skato# permitted provided that the above copyright notice and this
743561Skato# paragraph and the following disclaimer are duplicated in all
843561Skato# such forms.
943561Skato#
1043561Skato# This software is provided "AS IS" and without any express or
1143561Skato# implied warranties, including, without limitation, the implied
1243561Skato# warranties of merchantability and fitness for a particular
1343561Skato# purpose.
1443561Skato#
1543561Skato
1650477Speter# $FreeBSD: head/sys/boot/pc98/btx/btx/btx.S 52202 1999-10-13 10:55:56Z kato $
1743561Skato
1843561Skato#
1943561Skato# Memory layout.
2043561Skato#
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
3243561Skato#
3343561Skato# Paging control.
3443561Skato#
3543561Skato		.set PAG_SIZ,0x1000		# Page size
3643561Skato		.set PAG_CNT,0x1000		# Pages to map
3743561Skato#
3843561Skato# Segment selectors.
3943561Skato#
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
4743561Skato#
4843561Skato# Task state segment fields.
4943561Skato#
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
5443561Skato#
5543561Skato# System calls.
5643561Skato#
5743561Skato		.set SYS_EXIT,0x0		# Exit
5843561Skato		.set SYS_EXEC,0x1		# Exec
5943561Skato#
6043561Skato# V86 constants.
6143561Skato#
6243561Skato		.set V86_FLG,0x208eff		# V86 flag mask
6343561Skato		.set V86_STK,0x400		# V86 stack allowance
6443561Skato#
6543561Skato# Dump format control bytes.
6643561Skato#
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
7143561Skato#
7243561Skato# Screen defaults and assumptions.
7343561Skato#
7443561Skato.`ifdef' PC98
7543561Skato		.set SCR_MAT,0xe1		# Mode/attribute
7643561Skato.else
7743561Skato		.set SCR_MAT,0x7		# Mode/attribute
7843561Skato.endif
7943561Skato		.set SCR_COL,0x50		# Columns per row
8043561Skato		.set SCR_ROW,0x19		# Rows per screen
8143561Skato#
8243561Skato# BIOS Data Area locations.
8343561Skato#
8443561Skato.`ifdef' PC98
8543561Skato		.set BDA_MEM,0xa1501		# Free memory
8643561Skato		.set BDA_POS,0xa153e		# Cursor position
8743561Skato.else
8843561Skato		.set BDA_MEM,0x413		# Free memory
8943561Skato		.set BDA_SCR,0x449		# Video mode
9043561Skato		.set BDA_POS,0x450		# Cursor position
9143561Skato.endif
9243561Skato#
9343561Skato# Derivations, for brevity.
9443561Skato#
9543561Skato		.set _ESP0H,MEM_ESP0>>0x8	# Byte 1 of ESP0
9643561Skato		.set _ESP1H,MEM_ESP1>>0x8	# Byte 1 of ESP1
9743561Skato		.set _TSSIO,MEM_MAP-MEM_TSS	# TSS I/O base
9843561Skato		.set _TSSLM,MEM_DIR-MEM_TSS-1	# TSS limit
9943561Skato		.set _IDTLM,MEM_TSS-MEM_IDT-1	# IDT limit
10043561Skato#
10143561Skato# Code segment.
10243561Skato#
10343561Skato		.globl start
10443561Skatostart:						# Start of code
10543561Skato#
10643561Skato# BTX header.
10743561Skato#
10843561Skatobtx_hdr:	.byte 0xeb			# Machine ID
10943561Skato		.byte 0xe			# Header size
11043561Skato		.ascii "BTX"			# Magic
11143561Skato		.byte 0x1			# Major version
11244368Skato		.byte 0x1			# Minor version
11343561Skato		.byte 0x0			# Flags
11443561Skato		.word PAG_CNT-MEM_ORG>>0xc	# Paging control
11543561Skato		.word break-start		# Text size
11643561Skato		.long 0x0			# Entry address
11743561Skato#
11843561Skato# Initialization routine.
11943561Skato#
12043561Skatoinit:		cli				# Disable interrupts
12143561Skato		xorl %eax,%eax			# Zero/segment
12243561Skato		movl %ax,%ss			# Set up
12343561Skato		movwir(MEM_ESP0,_sp)		#  stack
12443561Skato		movl %ax,%es			# Address
12543561Skato		movl %ax,%ds			#  data
12643561Skato		pushw $0x2			# Clear
12743561Skato		popfw				#  flags
12843561Skato#
12943561Skato# Initialize memory.
13043561Skato#
13143561Skato		movwir(MEM_IDT,_di)		# Memory to initialize
13243561Skato		movwir((MEM_ORG-MEM_IDT)/2,_cx) # Words to zero
13343561Skato		pushl %edi			# Save
13443561Skato		rep				# Zero-fill
13543561Skato		stosl				#  memory
13643561Skato		popl %edi			# Restore
13743561Skato#
13843561Skato# Create IDT.
13943561Skato#
14043561Skato		movwir(idtctl,_si)		# Control string
14143561Skatoinit.1: 	lodsb				# Get entry
14243561Skato		cwde				#  count
14343561Skato		xchgl %eax,%ecx 		#  as word
14443561Skato		jecxz init.4			# If done
14543561Skato		lodsb				# Get segment
14643561Skato		xchgl %eax,%edx 		#  P:DPL:type
14743561Skato		lodsl				# Get control
14843561Skato		xchgl %eax,%ebx 		#  set
14943561Skato		lodsl				# Get handler offset
15043561Skato		movb $SEL_SCODE,%dh		# Segment selector
15143561Skatoinit.2: 	shrl %ebx			# Handle this int?
15243561Skato		jnc init.3			# No
15343561Skato		movwr0(_ax,_di_)		# Set handler offset
15443561Skato		movbr1(_dh,0x2,_di_)		#  and selector
15543561Skato		movbr1(_dl,0x5,_di_)		# Set P:DPL:type
15643561Skato		addwia(0x4)			# Next handler
15743561Skatoinit.3: 	leaw1r(0x8,_di_,_di)		# Next entry
15843561Skato		loop init.2			# Till set done
15943561Skato		jmp init.1			# Continue
16043561Skato#
16143561Skato# Initialize TSS.
16243561Skato#
16343561Skatoinit.4: 	movbi1(_ESP0H,TSS_ESP0+1,_di_)	# Set ESP0
16443561Skato		movbi1(SEL_SDATA,TSS_SS0,_di_)	# Set SS0
16543561Skato		movbi1(_ESP1H,TSS_ESP1+1,_di_)	# Set ESP1
16643561Skato		movbi1(_TSSIO,TSS_MAP,_di_)	# Set I/O bit map base
16752202Skatoifdef(`PAGING',`
16843561Skato#
16943561Skato# Create page directory.
17043561Skato#
17143561Skato		xorw %dx,%dx			# Page
17243561Skato		movb $PAG_SIZ>>0x8,%dh		#  size
17343561Skato		xorw %ax,%ax			# Zero
17443561Skato		movwir(MEM_DIR,_di)		# Page directory
17543561Skato		movb $PAG_CNT>>0xa,%cl		# Entries
17643561Skato		movwir(MEM_TBL|0x7,_ax) 	# First entry
17743561Skatoinit.5: 	stosw				# Write entry
17843561Skato		addl %edx,%eax			# To next
17943561Skato		loop init.5			# Till done
18043561Skato#
18143561Skato# Create page tables.
18243561Skato#
18343561Skato		movwir(MEM_TBL,_di)		# Page table
18443561Skato		movb $PAG_CNT>>0x8,%ch		# Entries
18543561Skato		xorl %eax,%eax			# Start address
18643561Skatoinit.6: 	movb $0x7,%al			# Set U:W:P flags
18743561Skato		cmpwmr(btx_hdr+0x8,_cx) 	# Standard user page?
18843561Skato		jb init.7			# Yes
18943561Skato		cmpwir(PAG_CNT-MEM_BTX>>0xc,_cx)# BTX memory?
19043561Skato		jae init.7			# No or first page
19143561Skato		andb $~0x2,%al			# Clear W flag
19243561Skato		cmpwir(PAG_CNT-MEM_USR>>0xc,_cx)# User page zero?
19343561Skato		jne init.7			# No
19443561Skato		tstbim(0x80,btx_hdr+0x7)	# Unmap it?
19543561Skato		jz init.7			# No
19643561Skato		andb $~0x1,%al			# Clear P flag
19743561Skatoinit.7: 	stosw				# Set entry
19843561Skato		addw %dx,%ax			# Next address
19943561Skato		loop init.6			# Till done
20052202Skato')
20143561Skato#
20243561Skato# Bring up the system.
20343561Skato#
20443561Skato		movwir(0x2820,_bx)		# Set protected mode
20543561Skato		callwi(setpic)			#  IRQ offsets
20643561Skato		lidtwm(idtdesc) 		# Set IDT
20752202Skatoifdef(`PAGING',`
20843561Skato		xorw %ax,%ax			# Set base
20943561Skato		movb $MEM_DIR>>0x8,%ah		#  of page
21043561Skato		movl %eax,%cr3			#  directory
21152202Skato')
21243561Skato		lgdtwm(gdtdesc) 		# Set GDT
21343561Skato		movl %cr0,%eax			# Switch to
21443561Skato		o16				#  protected mode
21552202Skato		orl $0x01,%eax			#
21652202Skato		movl %eax,%cr0			#
21743561Skato		jmpfwi(SEL_SCODE,init.8)	# To 32-bit code
21843561Skatoinit.8: 	xorl %ecx,%ecx			# Zero
21943561Skato		movb $SEL_SDATA,%cl		# To 32-bit
22043561Skato		movl %cx,%ss			#  stack
22143561Skato#
22243561Skato# Launch user task.
22343561Skato#
22443561Skato		movb $SEL_TSS,%cl		# Set task
22543561Skato		ltrl %ecx			#  register
22643561Skato		movl $MEM_USR,%edx		# User base address
22743561Skato		movzwl %ss:BDA_MEM,%eax 	# Get free memory
22843561Skato.`ifdef' PC98
22943561Skato		andl $0x7,%eax
23043561Skato		incl %eax
23143561Skato		shll $0x11,%eax			# To bytes
23243561Skato.else
23343561Skato		shll $0xa,%eax			# To bytes
23443561Skato.endif
23543561Skato		subl $0x1000,%eax		# Less arg space
23643561Skato		subl %edx,%eax			# Less base
23743561Skato		movb $SEL_UDATA,%cl		# User data selector
23843561Skato		pushl %ecx			# Set SS
23943561Skato		pushl %eax			# Set ESP
24043561Skato		pushl $0x202			# Set flags (IF set)
24143561Skato		pushl $SEL_UCODE		# Set CS
24243561Skato		pushl btx_hdr+0xc		# Set EIP
24343561Skato		pushl %ecx			# Set GS
24443561Skato		pushl %ecx			# Set FS
24543561Skato		pushl %ecx			# Set DS
24643561Skato		pushl %ecx			# Set ES
24743561Skato		pushl %edx			# Set EAX
24843561Skato		movb $0x7,%cl			# Set remaining
24943561Skatoinit.9:		pushb $0x0			#  general
25043561Skato		loop init.9			#  registers
25143561Skato		popa				#  and initialize
25243561Skato		popl %es			# Initialize
25343561Skato		popl %ds			#  user
25443561Skato		popl %fs			#  segment
25543561Skato		popl %gs			#  registers
25643561Skato		iret				# To user mode
25743561Skato#
25843561Skato# Exit routine.
25943561Skato#
26043561Skatoexit:		cli				# Disable interrupts
26143561Skato		movl $MEM_ESP0,%esp		# Clear stack
26252202Skatoifdef(`PAGING',`
26343561Skato#
26443561Skato# Turn off paging.
26543561Skato#
26643561Skato		movl %cr0,%eax			# Get CR0
26743561Skato		andl $~0x80000000,%eax		# Disable
26843561Skato		movl %eax,%cr0			#  paging
26952202Skato')
27043561Skato		xorl %ecx,%ecx			# Zero
27152202Skatoifdef(`PAGING',`
27243561Skato		movl %ecx,%cr3			# Flush TLB
27352202Skato')
27443561Skato#
27543561Skato# To 16 bits.
27643561Skato#
27743561Skato		o16				# Reload
27843561Skato		jmpfwi(SEL_RCODE,exit.1)	#  CS
27943561Skatoexit.1: 	movb $SEL_RDATA,%cl		# 16-bit selector
28043561Skato		movl %cx,%ss			# Reload SS
28143561Skato		movl %cx,%ds			# Load
28243561Skato		movl %cx,%es			#  remaining
28343561Skato		movl %cx,%fs			#  segment
28443561Skato		movl %cx,%gs			#  registers
28543561Skato#
28643561Skato# To real-address mode.
28743561Skato#
28843561Skato		decl %eax			# Switch to
28943561Skato		movl %eax,%cr0			#  real mode
29043561Skato		jmpfwi(0x0,exit.2)		# Reload CS
29143561Skatoexit.2: 	xorl %eax,%eax			# Real mode segment
29243561Skato		movl %ax,%ss			# Reload SS
29343561Skato		movl %ax,%ds			# Address data
29443561Skato.`ifdef' PC98
29543561Skato		movwir(0x1008,_bx)		# Set real mode
29643561Skato.else
29743561Skato		movwir(0x7008,_bx)		# Set real mode
29843561Skato.endif
29943561Skato		callwi(setpic)			#  IRQ offsets
30043561Skato		lidtwm(ivtdesc) 		# Set IVT
30143561Skato#
30243561Skato# Reboot or await reset.
30343561Skato#
30443561Skato		sti				# Enable interrupts
30543561Skato		tstbim(0x1,btx_hdr+0x7)		# Reboot?
30643561Skatoexit.3:		jz exit.3			# No
30743561Skato.`ifdef' PC98
30843561Skato		movb $0xa0,%al
30943561Skato		outb %al,$0x35
31043561Skato		movb 0,%al
31143561Skato		outb %al,$0xf0
31243561Skatoexit.4:		jmp exit.4
31343561Skato.else
31443561Skato		int $0x19			# BIOS: Reboot
31543561Skato.endif
31643561Skato#
31743561Skato# Set IRQ offsets by reprogramming 8259A PICs.
31843561Skato#
31943561Skato.`ifdef' PC98
32043561Skatosetpic: 	inb $0x02,%al			# Save master
32143561Skato		pushl %eax			#  IMR
32243561Skato		inb $0x0a,%al			# Save slave
32343561Skato		pushl %eax			#  IMR
32443561Skato		movb $0x11,%al			# ICW1 to
32543561Skato		outb %al,$0x00			#  master,
32643561Skato		outb %al,$0x08			#  slave
32743561Skato		movb %bl,%al			# ICW2 to
32843561Skato		outb %al,$0x02			#  master
32943561Skato		movb %bh,%al			# ICW2 to
33043561Skato		outb %al,$0x0a			#  slave
33143561Skato		movb $0x80,%al			# ICW3 to
33243561Skato		outb %al,$0x02			#  master
33343561Skato		movb $0x7,%al			# ICW3 to
33443561Skato		outb %al,$0x0a			#  slave
33543561Skato		movb $0x1d,%al			# ICW4 to
33643561Skato		outb %al,$0x02			#  master,
33743561Skato		movb $0x9,%al			# ICW4 to
33843561Skato		outb %al,$0x0a			#  slave
33943561Skato		popl %eax			# Restore slave
34043561Skato		outb %al,$0x0a			#  IMR
34143561Skato		popl %eax			# Restore master
34243561Skato		outb %al,$0x02			#  IMR
34343561Skato.else
34443561Skatosetpic: 	inb $0x21,%al			# Save master
34543561Skato		pushl %eax			#  IMR
34643561Skato		inb $0xa1,%al			# Save slave
34743561Skato		pushl %eax			#  IMR
34843561Skato		movb $0x11,%al			# ICW1 to
34943561Skato		outb %al,$0x20			#  master,
35043561Skato		outb %al,$0xa0			#  slave
35143561Skato		movb %bl,%al			# ICW2 to
35243561Skato		outb %al,$0x21			#  master
35343561Skato		movb %bh,%al			# ICW2 to
35443561Skato		outb %al,$0xa1			#  slave
35543561Skato		movb $0x4,%al			# ICW3 to
35643561Skato		outb %al,$0x21			#  master
35743561Skato		movb $0x2,%al			# ICW3 to
35843561Skato		outb %al,$0xa1			#  slave
35943561Skato		movb $0x1,%al			# ICW4 to
36043561Skato		outb %al,$0x21			#  master,
36143561Skato		outb %al,$0xa1			#  slave
36243561Skato		popl %eax			# Restore slave
36343561Skato		outb %al,$0xa1			#  IMR
36443561Skato		popl %eax			# Restore master
36543561Skato		outb %al,$0x21			#  IMR
36643561Skato.endif
36743561Skato		ret				# To caller
36843561Skato#
36943561Skato# Initiate return from V86 mode to user mode.
37043561Skato#
37143561Skatointhlt: 	hlt				# To supervisor mode
37243561Skato#
37343561Skato# Exception jump table.
37443561Skato#
37543561Skatointx00: 	pushb $0x0			# Int 0x0: #DE
37643561Skato		jmp ex_noc			# Divide error
37743561Skato		pushb $0x1			# Int 0x1: #DB
37843561Skato		jmp ex_noc			# Debug
37943561Skato		pushb $0x3			# Int 0x3: #BP
38043561Skato		jmp ex_noc			# Breakpoint
38143561Skato		pushb $0x4			# Int 0x4: #OF
38243561Skato		jmp ex_noc			# Overflow
38343561Skato		pushb $0x5			# Int 0x5: #BR
38443561Skato		jmp ex_noc			# BOUND range exceeded
38543561Skato		pushb $0x6			# Int 0x6: #UD
38643561Skato		jmp ex_noc			# Invalid opcode
38743561Skato		pushb $0x7			# Int 0x7: #NM
38843561Skato		jmp ex_noc			# Device not available
38943561Skato		pushb $0x8			# Int 0x8: #DF
39043561Skato		jmp except			# Double fault
39143561Skato		pushb $0xa			# Int 0xa: #TS
39243561Skato		jmp except			# Invalid TSS
39343561Skato		pushb $0xb			# Int 0xb: #NP
39443561Skato		jmp except			# Segment not present
39543561Skato		pushb $0xc			# Int 0xc: #SS
39643561Skato		jmp except			# Stack segment fault
39743561Skato		pushb $0xd			# Int 0xd: #GP
39843561Skato		jmp ex_v86			# General protection
39943561Skato		pushb $0xe			# Int 0xe: #PF
40043561Skato		jmp except			# Page fault
40143561Skatointx10: 	pushb $0x10			# Int 0x10: #MF
40243561Skato		jmp ex_noc			# Floating-point error
40343561Skato#
40443561Skato# Handle #GP exception.
40543561Skato#
40643561Skatoex_v86: 	testb $0x2,0x12(%esp,1) 	# V86 mode?
40743561Skato		jz except			# No
40843561Skato		jmp v86mon			# To monitor
40943561Skato#
41043561Skato# Save a zero error code.
41143561Skato#
41243561Skatoex_noc: 	pushl (%esp,1)			# Duplicate int no
41343561Skato		movb $0x0,0x4(%esp,1)		# Fake error code
41443561Skato#
41543561Skato# Handle exception.
41643561Skato#
41743561Skatoexcept: 	cld				# String ops inc
41843561Skato		pushl %ds			# Save
41943561Skato		pushl %es			#  most
42043561Skato		pusha				#  registers
42143561Skato		movb $0x6,%al			# Push loop count
42243561Skato		testb $0x2,0x3a(%esp,1) 	# V86 mode?
42343561Skato		jnz except.1			# Yes
42443561Skato		pushl %gs			# Set GS
42543561Skato		pushl %fs			# Set FS
42643561Skato		pushl %ds			# Set DS
42743561Skato		pushl %es			# Set ES
42843561Skato		movb $0x2,%al			# Push loop count
42943561Skato		cmpw $SEL_SCODE,0x44(%esp,1)	# Supervisor mode?
43043561Skato		jne except.1			# No
43143561Skato		pushl %ss			# Set SS
43243561Skato		leal 0x50(%esp,1),%eax		# Set
43343561Skato		pushl %eax			#  ESP
43443561Skato		jmp except.2			# Join common code
43543561Skatoexcept.1:	pushl 0x50(%esp,1)		# Set GS, FS, DS, ES
43643561Skato		decb %al			#  (if V86 mode), and
43743561Skato		jne except.1			#  SS, ESP
43843561Skatoexcept.2:	pushl $SEL_SDATA		# Set up
43943561Skato		popl %ds			#  to
44043561Skato		pushl %ds			#  address
44143561Skato		popl %es			#  data
44243561Skato		movl %esp,%ebx			# Stack frame
44343561Skato		movl $dmpfmt,%esi		# Dump format string
44443561Skato		movl $MEM_BUF,%edi		# Buffer
44543561Skato.`ifdef' PC98
44643561Skato		pushl %eax
44743561Skato		pushl %edx
44843561Skatowait.1:
44943561Skato		inb  $0x60,%al
45043561Skato		testb $0x04,%al
45143561Skato		jz   wait.1
45243561Skato		movb $0xe0,%al
45343561Skato		outb %al,$0x62
45443561Skatowait.2:
45543561Skato		inb  $0x60,%al
45643561Skato		testb $0x01,%al
45743561Skato		jz   wait.2
45843561Skato		xorl %edx,%edx
45943561Skato		inb  $0x62,%al
46043561Skato		movb %al,%dl
46143561Skato		inb  $0x62,%al
46243561Skato		movb %al,%dh
46343561Skato		inb  $0x62,%al
46443561Skato		inb  $0x62,%al
46543561Skato		inb  $0x62,%al
46643561Skato		movl %edx,%eax
46743561Skato		shlw $1,%ax
46843561Skato		movl $BDA_POS,%edx
46943561Skato		movw %ax,(%edx)
47043561Skato		popl  %edx
47143561Skato		popl  %eax
47243561Skato.endif
47343561Skato		pushl %edi			# Dump to
47443561Skato		call dump			#  buffer
47543561Skato		popl %esi			#  and
47643561Skato		call putstr			#  display
47743561Skato		leal 0x18(%esp,1),%esp		# Discard frame
47843561Skato		popa				# Restore
47943561Skato		popl %es			#  registers
48043561Skato		popl %ds			#  saved
48143561Skato		cmpb $0x3,(%esp,1)		# Breakpoint?
48243561Skato		je except.3			# Yes
48343561Skato		jmp exit			# Exit
48443561Skatoexcept.3:	leal 0x8(%esp,1),%esp		# Discard err, int no
48543561Skato		iret				# From interrupt
48643561Skato#
48743561Skato# Return to user mode from V86 mode.
48843561Skato#
48943561Skatointrtn: 	cld				# String ops inc
49043561Skato		pushl %ds			# Address
49143561Skato		popl %es			#  data
49243561Skato		leal 0x3c(%ebp),%edx		# V86 Segment registers
49343561Skato		movl MEM_TSS+TSS_ESP1,%esi	# Link stack pointer
49443561Skato		lodsl				# INT_V86 args pointer
49543561Skato		movl %esi,%ebx			# Saved exception frame
49643561Skato		testl %eax,%eax 		# INT_V86 args?
49743561Skato		jz intrtn.2			# No
49843561Skato		movl $MEM_USR,%edi		# User base
49943561Skato		movl 0x1c(%esi),%ebx		# User ESP
50043561Skato		movl %eax,(%edi,%ebx,1) 	# Restore to user stack
50143561Skato		leal 0x8(%edi,%eax,1),%edi	# Arg segment registers
50243561Skato		testb $0x4,-0x6(%edi)		# Return flags?
50343561Skato		jz intrtn.1			# No
50443561Skato		movl 0x30(%ebp),%eax		# Get V86 flags
50543561Skato		movw %ax,0x18(%esi)		# Set user flags
50643561Skatointrtn.1:	leal 0x10(%esi),%ebx		# Saved exception frame
50743561Skato		xchgl %edx,%esi 		# Segment registers
50843561Skato		movb $0x4,%cl			# Update seg regs
50943561Skato		rep				#  in INT_V86
51043561Skato		movsl				#  args
51143561Skatointrtn.2:	movl %edx,%esi			# Segment registers
51243561Skato		leal 0x28(%ebp),%edi		# Set up seg
51343561Skato		movb $0x4,%cl			#  regs for
51443561Skato		rep				#  later
51543561Skato		movsl				#  pop
51643561Skato		movl %ebx,%esi			# Restore exception
51743561Skato		movb $0x5,%cl			#  frame to
51843561Skato		rep				#  supervisor
51943561Skato		movsl				#  stack
52043561Skato		movl %esi,MEM_TSS+TSS_ESP1	# Link stack pointer
52143561Skato		popa				# Restore
52243561Skato		leal 0x8(%esp,1),%esp		# Discard err, int no
52343561Skato		popl %es			# Restore
52443561Skato		popl %ds			#  user
52543561Skato		popl %fs			#  segment
52643561Skato		popl %gs			#  registers
52743561Skato		iret				# To user mode
52843561Skato#
52943561Skato# V86 monitor.
53043561Skato#
53143561Skatov86mon: 	cld				# String ops inc
53243561Skato		pushl $SEL_SDATA		# Set up for
53343561Skato		popl %ds			#  flat addressing
53443561Skato		pusha				# Save registers
53543561Skato		movl %esp,%ebp			# Address stack frame
53643561Skato		movzwl 0x2c(%ebp),%edi		# Load V86 CS
53743561Skato		shll $0x4,%edi			# To linear
53843561Skato		movl 0x28(%ebp),%esi		# Load V86 IP
53943561Skato		addl %edi,%esi			# Code pointer
54043561Skato		xorl %ecx,%ecx			# Zero
54143561Skato		movb $0x2,%cl			# 16-bit operands
54243561Skato		xorl %eax,%eax			# Zero
54343561Skatov86mon.1:	lodsb				# Get opcode
54443561Skato		cmpb $0x66,%al			# Operand size prefix?
54543561Skato		jne v86mon.2			# No
54643561Skato		movb $0x4,%cl			# 32-bit operands
54743561Skato		jmp v86mon.1			# Continue
54843561Skatov86mon.2:	cmpb $0xf4,%al			# HLT?
54943561Skato		jne v86mon.3			# No
55043561Skato		cmpl $inthlt+0x1,%esi		# Is inthlt?
55144368Skato		jne v86mon.7			# No (ignore)
55243561Skato		jmp intrtn			# Return to user mode
55344368Skatov86mon.3:	cmpb $0xf,%al			# Is
55444368Skato		jne v86mon.4			#  this
55544368Skato		cmpb $0x20,(%esi)		#  a
55644368Skato		jne v86mon.4			#  MOV EAX,CR0
55744368Skato		cmpb $0xc0,0x1(%esi)		#  instruction?
55844368Skato		je v86mov			# Yes
55944368Skatov86mon.4:	cmpb $0xfa,%al			# CLI?
56043561Skato		je v86cli			# Yes
56143561Skato		cmpb $0xfb,%al			# STI?
56243561Skato		je v86sti			# Yes
56343561Skato		movzwl 0x38(%ebp),%ebx		# Load V86 SS
56443561Skato		shll $0x4,%ebx			# To offset
56543561Skato		pushl %ebx			# Save
56643561Skato		addl 0x34(%ebp),%ebx		# Add V86 SP
56743561Skato		movl 0x30(%ebp),%edx		# Load V86 flags
56843561Skato		cmpb $0x9c,%al			# PUSHF/PUSHFD?
56943561Skato		je v86pushf			# Yes
57043561Skato		cmpb $0x9d,%al			# POPF/POPFD?
57143561Skato		je v86popf			# Yes
57243561Skato		cmpb $0xcd,%al			# INT imm8?
57343561Skato		je v86intn			# Yes
57443561Skato		cmpb $0xcf,%al			# IRET/IRETD?
57543561Skato		je v86iret			# Yes
57643561Skato		popl %ebx			# Restore
57743561Skato		popa				# Restore
57843561Skato		jmp except			# Handle exception
57944368Skatov86mon.5:	movl %edx,0x30(%ebp)		# Save V86 flags
58044368Skatov86mon.6:	popl %edx			# V86 SS adjustment
58143561Skato		subl %edx,%ebx			# Save V86
58243561Skato		movl %ebx,0x34(%ebp)		#  SP
58344368Skatov86mon.7:	subl %edi,%esi			# From linear
58443561Skato		movl %esi,0x28(%ebp)		# Save V86 IP
58543561Skato		popa				# Restore
58643561Skato		leal 0x8(%esp,1),%esp		# Discard int no, error
58743561Skato		iret				# To V86 mode
58843561Skato#
58944368Skato# Emulate MOV EAX,CR0.
59044368Skato#
59144368Skatov86mov: 	movl %cr0,%eax			# CR0 to
59244368Skato		movl %eax,0x1c(%ebp)		#  saved EAX
59344368Skato		incl %esi			# Adjust
59444368Skato		incl %esi			#  IP
59544368Skato		jmp v86mon.7			# Finish up
59644368Skato#
59743561Skato# Emulate CLI.
59843561Skato#
59943561Skatov86cli: 	andb $~0x2,0x31(%ebp)		# Clear IF
60044368Skato		jmp v86mon.7			# Finish up
60143561Skato#
60243561Skato# Emulate STI.
60343561Skato#
60443561Skatov86sti: 	orb $0x2,0x31(%ebp)		# Set IF
60544368Skato		jmp v86mon.7			# Finish up
60643561Skato#
60743561Skato# Emulate PUSHF/PUSHFD.
60843561Skato#
60943561Skatov86pushf:	subl %ecx,%ebx			# Adjust SP
61043561Skato		cmpb $0x4,%cl			# 32-bit
61143561Skato		je v86pushf.1			# Yes
61243561Skato		o16				# 16-bit
61343561Skatov86pushf.1:	movl %edx,(%ebx)		# Save flags
61444368Skato		jmp v86mon.6			# Finish up
61543561Skato#
61643561Skato# Emulate IRET/IRETD.
61743561Skato#
61843561Skatov86iret:	movzwl (%ebx),%esi		# Load V86 IP
61943561Skato		movzwl 0x2(%ebx),%edi		# Load V86 CS
62043561Skato		leal 0x4(%ebx),%ebx		# Adjust SP
62143561Skato		movl %edi,0x2c(%ebp)		# Save V86 CS
62243561Skato		xorl %edi,%edi			# No ESI adjustment
62343561Skato#
62443561Skato# Emulate POPF/POPFD (and remainder of IRET/IRETD).
62543561Skato#
62643561Skatov86popf:	cmpb $0x4,%cl			# 32-bit?
62743561Skato		je v86popf.1			# Yes
62843561Skato		movl %edx,%eax			# Initialize
62943561Skato		o16				# 16-bit
63043561Skatov86popf.1:	movl (%ebx),%eax		# Load flags
63143561Skato		addl %ecx,%ebx			# Adjust SP
63243561Skato		andl $V86_FLG,%eax		# Merge
63343561Skato		andl $~V86_FLG,%edx		#  the
63443561Skato		orl %eax,%edx			#  flags
63544368Skato		jmp v86mon.5			# Finish up
63643561Skato#
63752202Skato# trap int 15, function 87
63852202Skato# reads %es:%si from saved registers on stack to find a GDT containing
63952202Skato# source and destination locations
64052202Skato# reads count of words from saved %cx
64152202Skato# returns success by setting %ah to 0
64243561Skato#
64352202Skatoint15_87:	pushl %eax			# Save
64452202Skato		pushl %ebx			#  some information
64552202Skato		pushl %esi			#  onto the stack.
64652202Skato		pushl %edi
64752202Skato		xorl %eax,%eax			# clean EAX
64852202Skato		xorl %ebx,%ebx			# clean EBX
64952202Skato		movl 0x4(%ebp),%esi		# Get user's ESI
65052202Skato		movl 0x3C(%ebp),%ebx		# store ES
65152202Skato		movw %si,%ax			# store SI
65252202Skato		shll $0x4,%ebx			# Make it a seg.
65352202Skato		addl %eax,%ebx			# ebx=(es<<4)+si
65452202Skato		movb 0x14(%ebx),%al		# Grab the
65552202Skato		movb 0x17(%ebx),%ah		#  necessary
65652202Skato		shll $0x10,%eax			#  information
65752202Skato		movw 0x12(%ebx),%ax		#  from
65852202Skato		movl %eax,%esi			#  the
65952202Skato		movb 0x1c(%ebx),%al		#  GDT in order to
66052202Skato		movb 0x1f(%ebx),%ah		#  have %esi offset
66152202Skato		shll $0x10,%eax			#  of source and %edi
66252202Skato		movw 0x1a(%ebx),%ax		#  of destination.
66352202Skato		movl %eax,%edi
66452202Skato		pushl %ds			# Make:
66552202Skato		popl %es			# es = ds
66652202Skato		pushl %ecx			# stash ECX
66752202Skato		xorl %ecx,%ecx			# highw of ECX is clear
66852202Skato		movw 0x18(%ebp),%cx		# Get user's ECX
66952202Skato		rep				# repeat...
67052202Skato		movsb				#  perform copy.
67152202Skato		popl %ecx			# Restore
67252202Skato		popl %edi
67352202Skato		popl %esi			#  previous
67452202Skato		popl %ebx			#  register
67552202Skato		popl %eax			#  values.
67652202Skato		movb $0x0,0x1d(%ebp)		# set ah = 0 to indicate
67752202Skato						#  success
67852202Skato		andb $0xfe,%dl			# clear CF
67952202Skato		jmp v86mon.5			# Finish up
68052202Skato
68152202Skato#
68252202Skato# Emulate INT imm8... also make sure to check if it's int 15/87
68352202Skato#
68443561Skatov86intn:	lodsb				# Get int no
68552202Skato		cmpb $0x15,%al			# is it int 15?
68652202Skato		jne v86intn.2			#  no, skip parse
68752202Skato		pushl %eax                      # stash EAX
68852202Skato		movl 0x1c(%ebp),%eax		# user's saved EAX
68952202Skato		cmpb $0x87,%ah			# is it our sub function?
69052202Skato		jne v86intn.1			#  no, don't handle it
69152202Skato		popl %eax			# get the stack straight
69252202Skato		jmp int15_87			# it's our cue
69352202Skatov86intn.1:	popl %eax			# restore EAX
69452202Skatov86intn.2:	subl %edi,%esi			# From
69543561Skato		shrl $0x4,%edi			#  linear
69643561Skato		movw %dx,-0x2(%ebx)		# Save flags
69743561Skato		movw %di,-0x4(%ebx)		# Save CS
69843561Skato		leal -0x6(%ebx),%ebx		# Adjust SP
69943561Skato		movw %si,(%ebx) 		# Save IP
70043561Skato		shll $0x2,%eax			# Scale
70143561Skato		movzwl (%eax),%esi		# Load IP
70243561Skato		movzwl 0x2(%eax),%edi		# Load CS
70343561Skato		movl %edi,0x2c(%ebp)		# Save CS
70443561Skato		xorl %edi,%edi			# No ESI adjustment
70543561Skato		andb $~0x3,%dh			# Clear IF and TF
70644368Skato		jmp v86mon.5			# Finish up
70743561Skato#
70843561Skato# Hardware interrupt jump table.
70943561Skato#
71043561Skatointx20: 	pushb $0x8			# Int 0x20: IRQ0
71143561Skato		jmp int_hw			# V86 int 0x8
71243561Skato		pushb $0x9			# Int 0x21: IRQ1
71343561Skato		jmp int_hw			# V86 int 0x9
71443561Skato		pushb $0xa			# Int 0x22: IRQ2
71543561Skato		jmp int_hw			# V86 int 0xa
71643561Skato		pushb $0xb			# Int 0x23: IRQ3
71743561Skato		jmp int_hw			# V86 int 0xb
71843561Skato		pushb $0xc			# Int 0x24: IRQ4
71943561Skato		jmp int_hw			# V86 int 0xc
72043561Skato		pushb $0xd			# Int 0x25: IRQ5
72143561Skato		jmp int_hw			# V86 int 0xd
72243561Skato		pushb $0xe			# Int 0x26: IRQ6
72343561Skato		jmp int_hw			# V86 int 0xe
72443561Skato		pushb $0xf			# Int 0x27: IRQ7
72543561Skato		jmp int_hw			# V86 int 0xf
72643561Skato.`ifdef' PC98
72743561Skato		pushb $0x10			# Int 0x28: IRQ8
72843561Skato		jmp int_hw			# V86 int 0x10
72943561Skato		pushb $0x11			# Int 0x29: IRQ9
73043561Skato		jmp int_hw			# V86 int 0x11
73143561Skato		pushb $0x12			# Int 0x2a: IRQ10
73243561Skato		jmp int_hw			# V86 int 0x12
73343561Skato		pushb $0x13			# Int 0x2b: IRQ11
73443561Skato		jmp int_hw			# V86 int 0x13
73543561Skato		pushb $0x14			# Int 0x2c: IRQ12
73643561Skato		jmp int_hw			# V86 int 0x14
73743561Skato		pushb $0x15			# Int 0x2d: IRQ13
73843561Skato		jmp int_hw			# V86 int 0x15
73943561Skato		pushb $0x16			# Int 0x2e: IRQ14
74043561Skato		jmp int_hw			# V86 int 0x16
74143561Skato		pushb $0x17			# Int 0x2f: IRQ15
74243561Skato		jmp int_hw			# V86 int 0x17
74343561Skato.else
74443561Skato		pushb $0x70			# Int 0x28: IRQ8
74543561Skato		jmp int_hw			# V86 int 0x70
74643561Skato		pushb $0x71			# Int 0x29: IRQ9
74743561Skato		jmp int_hw			# V86 int 0x71
74843561Skato		pushb $0x72			# Int 0x2a: IRQ10
74943561Skato		jmp int_hw			# V86 int 0x72
75043561Skato		pushb $0x73			# Int 0x2b: IRQ11
75143561Skato		jmp int_hw			# V86 int 0x73
75243561Skato		pushb $0x74			# Int 0x2c: IRQ12
75343561Skato		jmp int_hw			# V86 int 0x74
75443561Skato		pushb $0x75			# Int 0x2d: IRQ13
75543561Skato		jmp int_hw			# V86 int 0x75
75643561Skato		pushb $0x76			# Int 0x2e: IRQ14
75743561Skato		jmp int_hw			# V86 int 0x76
75843561Skato		pushb $0x77			# Int 0x2f: IRQ15
75943561Skato		jmp int_hw			# V86 int 0x77
76043561Skato.endif
76143561Skato#
76243561Skato# Reflect hardware interrupts.
76343561Skato#
76443561Skatoint_hw: 	testb $0x2,0xe(%esp,1)		# V86 mode?
76543561Skato		jz intusr			# No
76643561Skato		pushl $SEL_SDATA		# Address
76743561Skato		popl %ds			#  data
76843561Skato		xchgl %eax,(%esp,1)		# Swap EAX, int no
76943561Skato		pushl %ebp			# Address
77043561Skato		movl %esp,%ebp			#  stack frame
77143561Skato		pushl %ebx			# Save
77243561Skato		shll $0x2,%eax			# Get int
77343561Skato		movl (%eax),%eax		#  vector
77443561Skato		subl $0x6,0x14(%ebp)		# Adjust V86 ESP
77543561Skato		movzwl 0x18(%ebp),%ebx		# V86 SS
77643561Skato		shll $0x4,%ebx			#  * 0x10
77743561Skato		addl 0x14(%ebp),%ebx		#  + V86 ESP
77843561Skato		xchgw %ax,0x8(%ebp)		# Swap V86 IP
77943561Skato		rorl $0x10,%eax 		# Swap words
78043561Skato		xchgw %ax,0xc(%ebp)		# Swap V86 CS
78143561Skato		roll $0x10,%eax 		# Swap words
78243561Skato		movl %eax,(%ebx)		# CS:IP for IRET
78343561Skato		movl 0x10(%ebp),%eax		# V86 flags
78443561Skato		movw %ax,0x4(%ebx)		# Flags for IRET
78543561Skato		andb $~0x3,0x11(%ebp)		# Clear IF, TF
78643561Skato		popl %ebx			# Restore
78743561Skato		popl %ebp			#  saved
78843561Skato		popl %eax			#  registers
78943561Skato		iret				# To V86 mode
79043561Skato#
79143561Skato# Invoke V86 interrupt from user mode, with arguments.
79243561Skato#
79343561Skatointx31: 	stc				# Have btx_v86
79443561Skato		pushl %eax			# Missing int no
79543561Skato#
79643561Skato# Invoke V86 interrupt from user mode.
79743561Skato#
79843561Skatointusr: 	std				# String ops dec
79943561Skato		pushl %eax			# Expand
80043561Skato		pushl %eax			#  stack
80143561Skato		pushl %eax			#  frame
80243561Skato		pusha				# Save
80343561Skato		pushl %gs			# Save
80443561Skato		movl %esp,%eax			#  seg regs
80543561Skato		pushl %fs			#  and
80643561Skato		pushl %ds			#  point
80743561Skato		pushl %es			#  to them
80843561Skato		pushb $SEL_SDATA		# Set up
80943561Skato		popl %ds			#  to
81043561Skato		pushl %ds			#  address
81143561Skato		popl %es			#  data
81243561Skato		movl $MEM_USR,%ebx		# User base
81343561Skato		movl %ebx,%edx			#  address
81443561Skato		jc intusr.1			# If btx_v86
81543561Skato		xorl %edx,%edx			# Control flags
81643561Skato		xorl %ebp,%ebp			# btx_v86 pointer
81743561Skatointusr.1:	leal 0x50(%esp,1),%esi		# Base of frame
81843561Skato		pushl %esi			# Save
81943561Skato		addl -0x4(%esi),%ebx		# User ESP
82043561Skato		movl MEM_TSS+TSS_ESP1,%edi	# Link stack pointer
82143561Skato		leal -0x4(%edi),%edi		# Adjust for push
82243561Skato		xorl %ecx,%ecx			# Zero
82343561Skato		movb $0x5,%cl			# Push exception
82443561Skato		rep				#  frame on
82543561Skato		movsl				#  link stack
82643561Skato		xchgl %eax,%esi 		# Saved seg regs
82743561Skato		movl 0x40(%esp,1),%eax		# Get int no
82843561Skato		testl %edx,%edx 		# Have btx_v86?
82943561Skato		jz intusr.2			# No
83043561Skato		movl (%ebx),%ebp		# btx_v86 pointer
83143561Skato		movb $0x4,%cl			# Count
83243561Skato		addl %ecx,%ebx			# Adjust for pop
83343561Skato		rep				# Push saved seg regs
83443561Skato		movsl				#  on link stack
83543561Skato		addl %ebp,%edx			# Flatten btx_v86 ptr
83643561Skato		leal 0x14(%edx),%esi		# Seg regs pointer
83743561Skato		movl 0x4(%edx),%eax		# Get int no/address
83843561Skato		movzwl 0x2(%edx),%edx		# Get control flags
83943561Skatointusr.2:	movl %ebp,(%edi)		# Push btx_v86 and
84043561Skato		movl %edi,MEM_TSS+TSS_ESP1	#  save link stack ptr
84143561Skato		popl %edi			# Base of frame
84243561Skato		xchgl %eax,%ebp 		# Save intno/address
84343561Skato		movl 0x48(%esp,1),%eax		# Get flags
84443561Skato		testb $0x2,%dl			# Simulate CALLF?
84543561Skato		jnz intusr.3			# Yes
84643561Skato		decl %ebx			# Push flags
84743561Skato		decl %ebx			#  on V86
84843561Skato		movw %ax,(%ebx) 		#  stack
84943561Skatointusr.3:	movb $0x4,%cl			# Count
85043561Skato		subl %ecx,%ebx			# Push return address
85143561Skato		movl $inthlt,(%ebx)		#  on V86 stack
85243561Skato		rep				# Copy seg regs to
85343561Skato		movsl				#  exception frame
85443561Skato		xchgl %eax,%ecx 		# Save flags
85543561Skato		movl %ebx,%eax			# User ESP
85643561Skato		subl $V86_STK,%eax		# Less bytes
85743561Skato		ja intusr.4			#  to
85843561Skato		xorl %eax,%eax			#  keep
85943561Skatointusr.4:	shrl $0x4,%eax			# Gives segment
86043561Skato		stosl				# Set SS
86143561Skato		shll $0x4,%eax			# To bytes
86243561Skato		xchgl %eax,%ebx 		# Swap
86343561Skato		subl %ebx,%eax			# Gives offset
86443561Skato		stosl				# Set ESP
86543561Skato		xchgl %eax,%ecx 		# Get flags
86643561Skato		btsl $0x11,%eax 		# Set VM
86743561Skato		andb $~0x3,%ah			# Clear IF and TF
86843561Skato		stosl				# Set EFL
86943561Skato		xchgl %eax,%ebp 		# Get int no/address
87043561Skato		testb $0x1,%dl			# Address?
87143561Skato		jnz intusr.5			# Yes
87243561Skato		shll $0x2,%eax			# Scale
87343561Skato		movl (%eax),%eax		# Load int vector
87443561Skatointusr.5:	movl %eax,%ecx			# Save
87543561Skato		shrl $0x10,%eax 		# Gives segment
87643561Skato		stosl				# Set CS
87743561Skato		movw %cx,%ax			# Restore
87843561Skato		stosl				# Set EIP
87943561Skato		leal 0x10(%esp,1),%esp		# Discard seg regs
88043561Skato		popa				# Restore
88143561Skato		iret				# To V86 mode
88243561Skato#
88343561Skato# System Call.
88443561Skato#
88543561Skatointx30: 	cmpl $SYS_EXEC,%eax		# Exec system call?
88643561Skato		jne intx30.1			# No
88743561Skato		pushl %ss			# Set up
88843561Skato		popl %es			#  all
88943561Skato		pushl %es			#  segment
89043561Skato		popl %ds			#  registers
89143561Skato		pushl %ds			#  for the
89243561Skato		popl %fs			#  program
89343561Skato		pushl %fs			#  we're
89443561Skato		popl %gs			#  invoking
89543561Skato		movl $MEM_USR,%eax		# User base address
89643561Skato		addl 0xc(%esp,1),%eax		# Change to user
89743561Skato		leal 0x4(%eax),%esp		#  stack
89843561Skato		movl %cr0,%eax			# Turn
89943561Skato		andl $~0x80000000,%eax		#  off
90043561Skato		movl %eax,%cr0			#  paging
90143561Skato		xorl %eax,%eax			# Flush
90243561Skato		movl %eax,%cr3			#  TLB
90343561Skato		popl %eax			# Call
90443561Skato		call *%eax			#  program
90543561Skatointx30.1:	incb %ss:btx_hdr+0x7		# Flag reboot
90643561Skato		jmp exit			# Exit
90743561Skato#
90843561Skato# Dump structure [EBX] to [EDI], using format string [ESI].
90943561Skato#
91043561Skatodump.0: 	stosb				# Save char
91143561Skatodump:		lodsb				# Load char
91243561Skato		testb %al,%al			# End of string?
91343561Skato		jz dump.10			# Yes
91443561Skato		testb $0x80,%al 		# Control?
91543561Skato		jz dump.0			# No
91643561Skato		movb %al,%ch			# Save control
91743561Skato		movb $'=',%al			# Append
91843561Skato		stosb				#  '='
91943561Skato		lodsb				# Get offset
92043561Skato		pushl %esi			# Save
92143561Skato		movsbl %al,%esi 		# To
92243561Skato		addl %ebx,%esi			#  pointer
92343561Skato		testb $DMP_X16,%ch		# Dump word?
92443561Skato		jz dump.1			# No
92543561Skato		lodsw				# Get and
92643561Skato		call hex16			#  dump it
92743561Skatodump.1: 	testb $DMP_X32,%ch		# Dump long?
92843561Skato		jz dump.2			# No
92943561Skato		lodsl				# Get and
93043561Skato		call hex32			#  dump it
93143561Skatodump.2: 	testb $DMP_MEM,%ch		# Dump memory?
93243561Skato		jz dump.8			# No
93343561Skato		pushl %ds			# Save
93443561Skato		testb $0x2,0x52(%ebx)		# V86 mode?
93543561Skato		jnz dump.3			# Yes
93643561Skato		verrl 0x4(%esi) 		# Readable selector?
93743561Skato		jnz dump.3			# No
93843561Skato		ldsl (%esi),%esi		# Load pointer
93943561Skato		jmp dump.4			# Join common code
94043561Skatodump.3: 	lodsl				# Set offset
94143561Skato		xchgl %eax,%edx 		# Save
94243561Skato		lodsl				# Get segment
94343561Skato		shll $0x4,%eax			#  * 0x10
94443561Skato		addl %edx,%eax			#  + offset
94543561Skato		xchgl %eax,%esi 		# Set pointer
94643561Skatodump.4: 	movb $0x10,%cl			# Bytes to dump
94743561Skatodump.5: 	lodsb				# Get byte and
94843561Skato		call hex8			#  dump it
94943561Skato		decb %cl			# Keep count
95043561Skato		jz dump.7			# If done
95143561Skato		movb $'-',%al			# Separator
95243561Skato		cmpb $0x8,%cl			# Half way?
95343561Skato		je dump.6			# Yes
95443561Skato		movb $' ',%al			# Use space
95543561Skatodump.6: 	stosb				# Save separator
95643561Skato		jmp dump.5			# Continue
95743561Skatodump.7: 	popl %ds			# Restore
95843561Skatodump.8: 	popl %esi			# Restore
95943561Skato		movb $0xa,%al			# Line feed
96043561Skato		testb $DMP_EOL,%ch		# End of line?
96143561Skato		jnz dump.9			# Yes
96243561Skato		movb $' ',%al			# Use spaces
96343561Skato		stosb				# Save one
96443561Skatodump.9: 	jmp dump.0			# Continue
96543561Skatodump.10:	stosb				# Terminate string
96643561Skato		ret				# To caller
96743561Skato#
96843561Skato# Convert EAX, AX, or AL to hex, saving the result to [EDI].
96943561Skato#
97043561Skatohex32:		pushl %eax			# Save
97143561Skato		shrl $0x10,%eax 		# Do upper
97243561Skato		call hex16			#  16
97343561Skato		popl %eax			# Restore
97443561Skatohex16:		call hex16.1			# Do upper 8
97543561Skatohex16.1:	xchgb %ah,%al			# Save/restore
97643561Skatohex8:		pushl %eax			# Save
97743561Skato		shrb $0x4,%al			# Do upper
97843561Skato		call hex8.1			#  4
97943561Skato		popl %eax			# Restore
98043561Skatohex8.1: 	andb $0xf,%al			# Get lower 4
98143561Skato		cmpb $0xa,%al			# Convert
98243561Skato		sbbb $0x69,%al			#  to hex
98343561Skato		das				#  digit
98443561Skato		orb $0x20,%al			# To lower case
98543561Skato		stosb				# Save char
98643561Skato		ret				# (Recursive)
98743561Skato#
98843561Skato# Output zero-terminated string [ESI] to the console.
98943561Skato#
99043561Skatoputstr.0:	call putchr			# Output char
99143561Skatoputstr: 	lodsb				# Load char
99243561Skato		testb %al,%al			# End of string?
99343561Skato		jnz putstr.0			# No
99443561Skato		ret				# To caller
99543561Skato#
99643561Skato# Output character AL to the console.
99743561Skato#
99843561Skatoputchr: 	pusha				# Save
99943561Skato		xorl %ecx,%ecx			# Zero for loops
100043561Skato		movb $SCR_MAT,%ah		# Mode/attribute
100143561Skato		movl $BDA_POS,%ebx		# BDA pointer
100243561Skato		movw (%ebx),%dx 		# Cursor position
100343561Skato.`ifdef' PC98
100443561Skato		movl $0xa0000,%edi
100543561Skato.else
100643561Skato		movl $0xb8000,%edi		# Regen buffer (color)
100743561Skato		cmpb %ah,BDA_SCR-BDA_POS(%ebx)	# Mono mode?
100843561Skato		jne putchr.1			# No
100943561Skato		xorw %di,%di			# Regen buffer (mono)
101043561Skato.endif
101143561Skatoputchr.1:	cmpb $0xa,%al			# New line?
101243561Skato		je putchr.2			# Yes
101343561Skato.`ifdef' PC98
101443561Skato		movw %dx,%cx
101543561Skato		movb %al,(%edi,%ecx,1)		# Write char
101643561Skato		addl $0x2000,%ecx
101743561Skato		movb %ah,(%edi,%ecx,1)		# Write attr
101843561Skato		addw $0x02,%dx
101943561Skato		jmp putchr.3
102043561Skatoputchr.2:	movw %dx,%ax
102143561Skato		movb $SCR_COL*2,%dl
102243561Skato		div %dl
102343561Skato		incb %al
102443561Skato		mul %dl
102543561Skato		movw %ax,%dx
102643561Skatoputchr.3:	cmpw $SCR_ROW*SCR_COL*2,%dx
102743561Skato.else
102843561Skato		xchgl %eax,%ecx 		# Save char
102943561Skato		movb $SCR_COL,%al		# Columns per row
103043561Skato		mulb %dh			#  * row position
103143561Skato		addb %dl,%al			#  + column
103243561Skato		adcb $0x0,%ah			#  position
103343561Skato		shll %eax			#  * 2
103443561Skato		xchgl %eax,%ecx 		# Swap char, offset
103543561Skato		movw %ax,(%edi,%ecx,1)		# Write attr:char
103643561Skato		incl %edx			# Bump cursor
103743561Skato		cmpb $SCR_COL,%dl		# Beyond row?
103843561Skato		jb putchr.3			# No
103943561Skatoputchr.2:	xorb %dl,%dl			# Zero column
104043561Skato		incb %dh			# Bump row
104143561Skatoputchr.3:	cmpb $SCR_ROW,%dh		# Beyond screen?
104243561Skato.endif
104343561Skato		jb putchr.4			# No
104443561Skato		leal 2*SCR_COL(%edi),%esi	# New top line
104543561Skato		movw $(SCR_ROW-1)*SCR_COL/2,%cx # Words to move
104643561Skato		rep				# Scroll
104743561Skato		movsl				#  screen
104843561Skato		movb $' ',%al			# Space
104943561Skato.`ifdef' PC98
105043561Skato		xorb %ah,%ah
105143561Skato.endif
105243561Skato		movb $SCR_COL,%cl		# Columns to clear
105343561Skato		rep				# Clear
105443561Skato		stosw				#  line
105543561Skato.`ifdef' PC98
105643561Skato		movw $(SCR_ROW-1)*SCR_COL*2,%dx
105743561Skato.else
105843561Skato		movb $SCR_ROW-1,%dh		# Bottom line
105943561Skato.endif
106043561Skatoputchr.4:	movw %dx,(%ebx) 		# Update position
106143561Skato		popa				# Restore
106243561Skato		ret				# To caller
106343561Skato
106443561Skato		.p2align 4
106543561Skato#
106643561Skato# Global descriptor table.
106743561Skato#
106843561Skatogdt:		.word 0x0,0x0,0x0,0x0		# Null entry
106943561Skato		.word 0xffff,0x0,0x9a00,0xcf	# SEL_SCODE
107043561Skato		.word 0xffff,0x0,0x9200,0xcf	# SEL_SDATA
107143561Skato		.word 0xffff,0x0,0x9a00,0x0	# SEL_RCODE
107243561Skato		.word 0xffff,0x0,0x9200,0x0	# SEL_RDATA
107343561Skato		.word 0xffff,MEM_USR,0xfa00,0xcf# SEL_UCODE
107443561Skato		.word 0xffff,MEM_USR,0xf200,0xcf# SEL_UDATA
107543561Skato		.word _TSSLM,MEM_TSS,0x8900,0x0 # SEL_TSS
107643561Skatogdt.1:
107743561Skato#
107843561Skato# Pseudo-descriptors.
107943561Skato#
108043561Skatogdtdesc:	.word gdt.1-gdt-1,gdt,0x0	# GDT
108143561Skatoidtdesc:	.word _IDTLM,MEM_IDT,0x0	# IDT
108243561Skatoivtdesc:	.word 0x400-0x0-1,0x0,0x0	# IVT
108343561Skato#
108443561Skato# IDT construction control string.
108543561Skato#
108643561Skatoidtctl: 	.byte 0x10,  0x8e		# Int 0x0-0xf
108743561Skato		.word 0x7dfb,intx00		#  (exceptions)
108843561Skato		.byte 0x10,  0x8e		# Int 0x10
108943561Skato		.word 0x1,   intx10		#  (exception)
109043561Skato		.byte 0x10,  0x8e		# Int 0x20-0x2f
109143561Skato		.word 0xffff,intx20		#  (hardware)
109243561Skato		.byte 0x1,   0xee		# int 0x30
109343561Skato		.word 0x1,   intx30		#  (system call)
109443561Skato		.byte 0x2,   0xee		# Int 0x31-0x32
109543561Skato		.word 0x1,   intx31		#  (V86, null)
109643561Skato		.byte 0x0			# End of string
109743561Skato#
109843561Skato# Dump format string.
109943561Skato#
110043561Skatodmpfmt: 	.byte '\n'			# "\n"
110143561Skato		.ascii "int"			# "int="
110243561Skato		.byte 0x80|DMP_X32,	   0x40 # "00000000  "
110343561Skato		.ascii "err"			# "err="
110443561Skato		.byte 0x80|DMP_X32,	   0x44 # "00000000  "
110543561Skato		.ascii "efl"			# "efl="
110643561Skato		.byte 0x80|DMP_X32,	   0x50 # "00000000  "
110743561Skato		.ascii "eip"			# "eip="
110843561Skato		.byte 0x80|DMP_X32|DMP_EOL,0x48 # "00000000\n"
110943561Skato		.ascii "eax"			# "eax="
111043561Skato		.byte 0x80|DMP_X32,	   0x34 # "00000000  "
111143561Skato		.ascii "ebx"			# "ebx="
111243561Skato		.byte 0x80|DMP_X32,	   0x28 # "00000000  "
111343561Skato		.ascii "ecx"			# "ecx="
111443561Skato		.byte 0x80|DMP_X32,	   0x30 # "00000000  "
111543561Skato		.ascii "edx"			# "edx="
111643561Skato		.byte 0x80|DMP_X32|DMP_EOL,0x2c # "00000000\n"
111743561Skato		.ascii "esi"			# "esi="
111843561Skato		.byte 0x80|DMP_X32,	   0x1c # "00000000  "
111943561Skato		.ascii "edi"			# "edi="
112043561Skato		.byte 0x80|DMP_X32,	   0x18 # "00000000  "
112143561Skato		.ascii "ebp"			# "ebp="
112243561Skato		.byte 0x80|DMP_X32,	   0x20 # "00000000  "
112343561Skato		.ascii "esp"			# "esp="
112443561Skato		.byte 0x80|DMP_X32|DMP_EOL,0x0	# "00000000\n"
112543561Skato		.ascii "cs"			# "cs="
112643561Skato		.byte 0x80|DMP_X16,	   0x4c # "0000  "
112743561Skato		.ascii "ds"			# "ds="
112843561Skato		.byte 0x80|DMP_X16,	   0xc	# "0000  "
112943561Skato		.ascii "es"			# "es="
113043561Skato		.byte 0x80|DMP_X16,	   0x8	# "0000  "
113143561Skato		.ascii "  "			# "  "
113243561Skato		.ascii "fs"			# "fs="
113343561Skato		.byte 0x80|DMP_X16,	   0x10 # "0000  "
113443561Skato		.ascii "gs"			# "gs="
113543561Skato		.byte 0x80|DMP_X16,	   0x14 # "0000  "
113643561Skato		.ascii "ss"			# "ss="
113743561Skato		.byte 0x80|DMP_X16|DMP_EOL,0x4	# "0000\n"
113843561Skato		.ascii "cs:eip" 		# "cs:eip="
113943561Skato		.byte 0x80|DMP_MEM|DMP_EOL,0x48 # "00 00 ... 00 00\n"
114043561Skato		.ascii "ss:esp" 		# "ss:esp="
114143561Skato		.byte 0x80|DMP_MEM|DMP_EOL,0x0	# "00 00 ... 00 00\n"
114243561Skato		.asciz "System halted"		# End
114343561Skato#
114443561Skato# End of BTX memory.
114543561Skato#
114643561Skato		.p2align 4
114743561Skatobreak:
1148