btx.S revision 176645
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 176645 2008-02-28 17:33:06Z 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
28176645Snyan		.set MEM_TSS_END,0x3fff		# Page directory
2943561Skato		.set MEM_ORG,0x9000		# BTX code
3043561Skato		.set MEM_USR,0xa000		# Start of user memory
31128710Sru/*
32128710Sru * Paging control.
33128710Sru */
3443561Skato		.set PAG_SIZ,0x1000		# Page size
3543561Skato		.set PAG_CNT,0x1000		# Pages to map
36128710Sru/*
37128710Sru * Segment selectors.
38128710Sru */
3943561Skato		.set SEL_SCODE,0x8		# Supervisor code
4043561Skato		.set SEL_SDATA,0x10		# Supervisor data
4143561Skato		.set SEL_RCODE,0x18		# Real mode code
4243561Skato		.set SEL_RDATA,0x20		# Real mode data
4343561Skato		.set SEL_UCODE,0x28|3		# User code
4443561Skato		.set SEL_UDATA,0x30|3		# User data
4543561Skato		.set SEL_TSS,0x38		# TSS
46128710Sru/*
47128710Sru * Task state segment fields.
48128710Sru */
4943561Skato		.set TSS_ESP0,0x4		# PL 0 ESP
5043561Skato		.set TSS_SS0,0x8		# PL 0 SS
5143561Skato		.set TSS_ESP1,0xc		# PL 1 ESP
5243561Skato		.set TSS_MAP,0x66		# I/O bit map base
53128710Sru/*
54128710Sru * System calls.
55128710Sru */
5643561Skato		.set SYS_EXIT,0x0		# Exit
5743561Skato		.set SYS_EXEC,0x1		# Exec
58128710Sru/*
59128710Sru * V86 constants.
60128710Sru */
6143561Skato		.set V86_FLG,0x208eff		# V86 flag mask
6243561Skato		.set V86_STK,0x400		# V86 stack allowance
63128710Sru/*
64128710Sru * Dump format control bytes.
65128710Sru */
6643561Skato		.set DMP_X16,0x1		# Word
6743561Skato		.set DMP_X32,0x2		# Long
6843561Skato		.set DMP_MEM,0x4		# Memory
6943561Skato		.set DMP_EOL,0x8		# End of line
70128710Sru/*
71128710Sru * Screen defaults and assumptions.
72128710Sru */
7343561Skato		.set SCR_MAT,0xe1		# Mode/attribute
7443561Skato		.set SCR_COL,0x50		# Columns per row
7543561Skato		.set SCR_ROW,0x19		# Rows per screen
76128710Sru/*
77128710Sru * BIOS Data Area locations.
78128710Sru */
7958871Skato		.set BDA_MEM,0x501		# Free memory
8058871Skato		.set BDA_KEYFLAGS,0x53a		# Keyboard shift-state flags
8158871Skato		.set BDA_POS,0x53e		# Cursor position
82128710Sru/*
83128710Sru * Derivations, for brevity.
84128710Sru */
8543561Skato		.set _ESP0H,MEM_ESP0>>0x8	# Byte 1 of ESP0
8643561Skato		.set _ESP1H,MEM_ESP1>>0x8	# Byte 1 of ESP1
8743561Skato		.set _TSSIO,MEM_MAP-MEM_TSS	# TSS I/O base
88176645Snyan		.set _TSSLM,MEM_TSS_END-MEM_TSS	# TSS limit
8943561Skato		.set _IDTLM,MEM_TSS-MEM_IDT-1	# IDT limit
90128710Sru/*
91128710Sru * Code segment.
92128710Sru */
9343561Skato		.globl start
9461064Snyan		.code16
9543561Skatostart:						# Start of code
96128710Sru/*
97128710Sru * BTX header.
98128710Sru */
9943561Skatobtx_hdr:	.byte 0xeb			# Machine ID
10043561Skato		.byte 0xe			# Header size
10143561Skato		.ascii "BTX"			# Magic
10243561Skato		.byte 0x1			# Major version
10344368Skato		.byte 0x1			# Minor version
10468358Snyan		.byte BTX_FLAGS			# Flags
10543561Skato		.word PAG_CNT-MEM_ORG>>0xc	# Paging control
10643561Skato		.word break-start		# Text size
10743561Skato		.long 0x0			# Entry address
108128710Sru/*
109128710Sru * Initialization routine.
110128710Sru */
11143561Skatoinit:		cli				# Disable interrupts
11261064Snyan		xor %ax,%ax			# Zero/segment
11361064Snyan		mov %ax,%ss			# Set up
11461064Snyan		mov $MEM_ESP0,%sp		#  stack
11561064Snyan		mov %ax,%es			# Address
11661064Snyan		mov %ax,%ds			#  data
11761064Snyan		pushl $0x2			# Clear
11861064Snyan		popfl				#  flags
119128710Sru/*
120128710Sru * Initialize memory.
121128710Sru */
12261064Snyan		mov $MEM_IDT,%di		# Memory to initialize
12361064Snyan		mov $(MEM_ORG-MEM_IDT)/2,%cx	# Words to zero
12461064Snyan		push %di			# Save
12543561Skato		rep				# Zero-fill
12661064Snyan		stosw				#  memory
12761064Snyan		pop %di				# Restore
128128710Sru/*
129128710Sru * Create IDT.
130128710Sru */
13161064Snyan		mov $idtctl,%si			# Control string
13243561Skatoinit.1: 	lodsb				# Get entry
13361064Snyan		cbw				#  count
13461064Snyan		xchg %ax,%cx			#  as word
13561064Snyan		jcxz init.4			# If done
13643561Skato		lodsb				# Get segment
13761064Snyan		xchg %ax,%dx	 		#  P:DPL:type
13861064Snyan		lodsw				# Get control
13961064Snyan		xchg %ax,%bx			#  set
14061064Snyan		lodsw				# Get handler offset
14161064Snyan		mov $SEL_SCODE,%dh		# Segment selector
14261064Snyaninit.2: 	shr %bx				# Handle this int?
14343561Skato		jnc init.3			# No
14461064Snyan		mov %ax,(%di)			# Set handler offset
14561064Snyan		mov %dh,0x2(%di)		#  and selector
14661064Snyan		mov %dl,0x5(%di)		# Set P:DPL:type
14761064Snyan		add $0x4,%ax			# Next handler
14861064Snyaninit.3: 	lea 0x8(%di),%di		# Next entry
14943561Skato		loop init.2			# Till set done
15043561Skato		jmp init.1			# Continue
151128710Sru/*
152128710Sru * Initialize TSS.
153128710Sru */
15461064Snyaninit.4: 	movb $_ESP0H,TSS_ESP0+1(%di)	# Set ESP0
15561064Snyan		movb $SEL_SDATA,TSS_SS0(%di)	# Set SS0
15661064Snyan		movb $_ESP1H,TSS_ESP1+1(%di)	# Set ESP1
15761064Snyan		movb $_TSSIO,TSS_MAP(%di)	# Set I/O bit map base
158128710Sru/*
159128710Sru * Bring up the system.
160128710Sru */
16161064Snyan		mov $0x2820,%bx			# Set protected mode
16261064Snyan		callw setpic			#  IRQ offsets
16361064Snyan		lidt idtdesc	 		# Set IDT
16461064Snyan		lgdt gdtdesc	 		# Set GDT
16561064Snyan		mov %cr0,%eax			# Switch to protected
166164114Snyan		inc %ax				#  mode
167128710Sru		mov %eax,%cr0			#
16861064Snyan		ljmp $SEL_SCODE,$init.8		# To 32-bit code
16961064Snyan		.code32
17043561Skatoinit.8: 	xorl %ecx,%ecx			# Zero
17143561Skato		movb $SEL_SDATA,%cl		# To 32-bit
17261064Snyan		movw %cx,%ss			#  stack
173128710Sru/*
174128710Sru * Launch user task.
175128710Sru */
17643561Skato		movb $SEL_TSS,%cl		# Set task
17761064Snyan		ltr %cx				#  register
17843561Skato		movl $MEM_USR,%edx		# User base address
17943561Skato		movzwl %ss:BDA_MEM,%eax 	# Get free memory
18043561Skato		andl $0x7,%eax
18143561Skato		incl %eax
18243561Skato		shll $0x11,%eax			# To bytes
18343561Skato		subl $0x1000,%eax		# Less arg space
18443561Skato		subl %edx,%eax			# Less base
18543561Skato		movb $SEL_UDATA,%cl		# User data selector
18643561Skato		pushl %ecx			# Set SS
18743561Skato		pushl %eax			# Set ESP
18861064Snyan		push $0x202			# Set flags (IF set)
18961064Snyan		push $SEL_UCODE			# Set CS
19043561Skato		pushl btx_hdr+0xc		# Set EIP
19143561Skato		pushl %ecx			# Set GS
19243561Skato		pushl %ecx			# Set FS
19343561Skato		pushl %ecx			# Set DS
19443561Skato		pushl %ecx			# Set ES
19543561Skato		pushl %edx			# Set EAX
19643561Skato		movb $0x7,%cl			# Set remaining
19761064Snyaninit.9:		push $0x0			#  general
19843561Skato		loop init.9			#  registers
199125780Snyan#ifdef BTX_SERIAL
20086497Snyan		call sio_init			# setup the serial console
201125780Snyan#endif
20243561Skato		popa				#  and initialize
20343561Skato		popl %es			# Initialize
20443561Skato		popl %ds			#  user
20543561Skato		popl %fs			#  segment
20643561Skato		popl %gs			#  registers
20743561Skato		iret				# To user mode
208128710Sru/*
209128710Sru * Exit routine.
210128710Sru */
21143561Skatoexit:		cli				# Disable interrupts
21243561Skato		movl $MEM_ESP0,%esp		# Clear stack
213128710Sru/*
214128710Sru * Turn off paging.
215128710Sru */
21643561Skato		movl %cr0,%eax			# Get CR0
21743561Skato		andl $~0x80000000,%eax		# Disable
21843561Skato		movl %eax,%cr0			#  paging
21943561Skato		xorl %ecx,%ecx			# Zero
22043561Skato		movl %ecx,%cr3			# Flush TLB
221128710Sru/*
222128710Sru * Restore the GDT in case we caught a kernel trap.
223128710Sru */
22476927Snyan		lgdt gdtdesc	 		# Set GDT
225128710Sru/*
226128710Sru * To 16 bits.
227128710Sru */
22861064Snyan		ljmpw $SEL_RCODE,$exit.1	# Reload CS
22961064Snyan		.code16
23061064Snyanexit.1: 	mov $SEL_RDATA,%cl		# 16-bit selector
23161064Snyan		mov %cx,%ss			# Reload SS
23261064Snyan		mov %cx,%ds			# Load
23361064Snyan		mov %cx,%es			#  remaining
23461064Snyan		mov %cx,%fs			#  segment
23561064Snyan		mov %cx,%gs			#  registers
236128710Sru/*
237128710Sru * To real-address mode.
238128710Sru */
23961064Snyan		dec %ax				# Switch to
24061064Snyan		mov %eax,%cr0			#  real mode
24161064Snyan		ljmp $0x0,$exit.2		# Reload CS
24261064Snyanexit.2: 	xor %ax,%ax			# Real mode segment
24361064Snyan		mov %ax,%ss			# Reload SS
24461064Snyan		mov %ax,%ds			# Address data
24561064Snyan		mov $0x1008,%bx			# Set real mode
24661064Snyan		callw setpic			#  IRQ offsets
24761064Snyan		lidt ivtdesc	 		# Set IVT
248128710Sru/*
249128710Sru * Reboot or await reset.
250128710Sru */
25143561Skato		sti				# Enable interrupts
25261064Snyan		testb $0x1,btx_hdr+0x7		# Reboot?
25343561Skatoexit.3:		jz exit.3			# No
25443561Skato		movb $0xa0,%al
25543561Skato		outb %al,$0x35
25643561Skato		movb 0,%al
25743561Skato		outb %al,$0xf0
25843561Skatoexit.4:		jmp exit.4
259128710Sru/*
260128710Sru * Set IRQ offsets by reprogramming 8259A PICs.
261128710Sru */
26261064Snyansetpic: 	in $0x02,%al			# Save master
26361064Snyan		push %ax			#  IMR
26461064Snyan		in $0x0a,%al			# Save slave
26561064Snyan		push %ax			#  IMR
26643561Skato		movb $0x11,%al			# ICW1 to
26743561Skato		outb %al,$0x00			#  master,
26843561Skato		outb %al,$0x08			#  slave
26943561Skato		movb %bl,%al			# ICW2 to
27043561Skato		outb %al,$0x02			#  master
27143561Skato		movb %bh,%al			# ICW2 to
27243561Skato		outb %al,$0x0a			#  slave
27343561Skato		movb $0x80,%al			# ICW3 to
27443561Skato		outb %al,$0x02			#  master
27543561Skato		movb $0x7,%al			# ICW3 to
27643561Skato		outb %al,$0x0a			#  slave
27743561Skato		movb $0x1d,%al			# ICW4 to
27843561Skato		outb %al,$0x02			#  master,
27943561Skato		movb $0x9,%al			# ICW4 to
28043561Skato		outb %al,$0x0a			#  slave
28161064Snyan		pop %ax				# Restore slave
28243561Skato		outb %al,$0x0a			#  IMR
28361064Snyan		pop %ax				# Restore master
28443561Skato		outb %al,$0x02			#  IMR
28561064Snyan		retw				# To caller
28661064Snyan		.code32
287128710Sru/*
288128710Sru * Initiate return from V86 mode to user mode.
289128710Sru */
29043561Skatointhlt: 	hlt				# To supervisor mode
291128710Sru/*
292128710Sru * Exception jump table.
293128710Sru */
29461064Snyanintx00: 	push $0x0			# Int 0x0: #DE
29543561Skato		jmp ex_noc			# Divide error
29661064Snyan		push $0x1			# Int 0x1: #DB
29743561Skato		jmp ex_noc			# Debug
29861064Snyan		push $0x3			# Int 0x3: #BP
29943561Skato		jmp ex_noc			# Breakpoint
30061064Snyan		push $0x4			# Int 0x4: #OF
30143561Skato		jmp ex_noc			# Overflow
30261064Snyan		push $0x5			# Int 0x5: #BR
30343561Skato		jmp ex_noc			# BOUND range exceeded
30461064Snyan		push $0x6			# Int 0x6: #UD
30543561Skato		jmp ex_noc			# Invalid opcode
30661064Snyan		push $0x7			# Int 0x7: #NM
30743561Skato		jmp ex_noc			# Device not available
30861064Snyan		push $0x8			# Int 0x8: #DF
30943561Skato		jmp except			# Double fault
31061064Snyan		push $0xa			# Int 0xa: #TS
31143561Skato		jmp except			# Invalid TSS
31261064Snyan		push $0xb			# Int 0xb: #NP
31343561Skato		jmp except			# Segment not present
31461064Snyan		push $0xc			# Int 0xc: #SS
31543561Skato		jmp except			# Stack segment fault
31661064Snyan		push $0xd			# Int 0xd: #GP
31743561Skato		jmp ex_v86			# General protection
31861064Snyan		push $0xe			# Int 0xe: #PF
31943561Skato		jmp except			# Page fault
32061064Snyanintx10: 	push $0x10			# Int 0x10: #MF
32143561Skato		jmp ex_noc			# Floating-point error
322128710Sru/*
323128710Sru * Handle #GP exception.
324128710Sru */
32543561Skatoex_v86: 	testb $0x2,0x12(%esp,1) 	# V86 mode?
32643561Skato		jz except			# No
32743561Skato		jmp v86mon			# To monitor
328128710Sru/*
329128710Sru * Save a zero error code.
330128710Sru */
33143561Skatoex_noc: 	pushl (%esp,1)			# Duplicate int no
33243561Skato		movb $0x0,0x4(%esp,1)		# Fake error code
333128710Sru/*
334128710Sru * Handle exception.
335128710Sru */
33643561Skatoexcept: 	cld				# String ops inc
33743561Skato		pushl %ds			# Save
33843561Skato		pushl %es			#  most
33943561Skato		pusha				#  registers
34043561Skato		movb $0x6,%al			# Push loop count
34143561Skato		testb $0x2,0x3a(%esp,1) 	# V86 mode?
34243561Skato		jnz except.1			# Yes
34343561Skato		pushl %gs			# Set GS
34443561Skato		pushl %fs			# Set FS
34543561Skato		pushl %ds			# Set DS
34643561Skato		pushl %es			# Set ES
34743561Skato		movb $0x2,%al			# Push loop count
34843561Skato		cmpw $SEL_SCODE,0x44(%esp,1)	# Supervisor mode?
34943561Skato		jne except.1			# No
35043561Skato		pushl %ss			# Set SS
35143561Skato		leal 0x50(%esp,1),%eax		# Set
35243561Skato		pushl %eax			#  ESP
35343561Skato		jmp except.2			# Join common code
35443561Skatoexcept.1:	pushl 0x50(%esp,1)		# Set GS, FS, DS, ES
35543561Skato		decb %al			#  (if V86 mode), and
35643561Skato		jne except.1			#  SS, ESP
35761064Snyanexcept.2:	push $SEL_SDATA			# Set up
35843561Skato		popl %ds			#  to
35943561Skato		pushl %ds			#  address
36043561Skato		popl %es			#  data
36143561Skato		movl %esp,%ebx			# Stack frame
36243561Skato		movl $dmpfmt,%esi		# Dump format string
36343561Skato		movl $MEM_BUF,%edi		# Buffer
36443561Skato		pushl %eax
36543561Skato		pushl %edx
36643561Skatowait.1:
36743561Skato		inb  $0x60,%al
36843561Skato		testb $0x04,%al
36943561Skato		jz   wait.1
37043561Skato		movb $0xe0,%al
37143561Skato		outb %al,$0x62
37243561Skatowait.2:
37343561Skato		inb  $0x60,%al
37443561Skato		testb $0x01,%al
37543561Skato		jz   wait.2
37643561Skato		xorl %edx,%edx
37743561Skato		inb  $0x62,%al
37843561Skato		movb %al,%dl
37943561Skato		inb  $0x62,%al
38043561Skato		movb %al,%dh
38143561Skato		inb  $0x62,%al
38243561Skato		inb  $0x62,%al
38343561Skato		inb  $0x62,%al
38443561Skato		movl %edx,%eax
38543561Skato		shlw $1,%ax
38643561Skato		movl $BDA_POS,%edx
38743561Skato		movw %ax,(%edx)
38843561Skato		popl  %edx
38943561Skato		popl  %eax
39043561Skato		pushl %edi			# Dump to
39143561Skato		call dump			#  buffer
39243561Skato		popl %esi			#  and
39343561Skato		call putstr			#  display
39443561Skato		leal 0x18(%esp,1),%esp		# Discard frame
39543561Skato		popa				# Restore
39643561Skato		popl %es			#  registers
39743561Skato		popl %ds			#  saved
39843561Skato		cmpb $0x3,(%esp,1)		# Breakpoint?
39943561Skato		je except.3			# Yes
40086497Snyan		cmpb $0x1,(%esp,1)		# Debug?
40186497Snyan		jne except.2a			# No
40286497Snyan		testl $0x100,0x10(%esp,1)	# Trap flag set?
40386497Snyan		jnz except.3			# Yes
40486497Snyanexcept.2a:	jmp exit			# Exit
40543561Skatoexcept.3:	leal 0x8(%esp,1),%esp		# Discard err, int no
40643561Skato		iret				# From interrupt
407128710Sru/*
408128710Sru * Return to user mode from V86 mode.
409128710Sru */
41043561Skatointrtn: 	cld				# String ops inc
41143561Skato		pushl %ds			# Address
41243561Skato		popl %es			#  data
41343561Skato		leal 0x3c(%ebp),%edx		# V86 Segment registers
41443561Skato		movl MEM_TSS+TSS_ESP1,%esi	# Link stack pointer
41543561Skato		lodsl				# INT_V86 args pointer
41643561Skato		movl %esi,%ebx			# Saved exception frame
41743561Skato		testl %eax,%eax 		# INT_V86 args?
41843561Skato		jz intrtn.2			# No
41943561Skato		movl $MEM_USR,%edi		# User base
42043561Skato		movl 0x1c(%esi),%ebx		# User ESP
42143561Skato		movl %eax,(%edi,%ebx,1) 	# Restore to user stack
42243561Skato		leal 0x8(%edi,%eax,1),%edi	# Arg segment registers
42343561Skato		testb $0x4,-0x6(%edi)		# Return flags?
42443561Skato		jz intrtn.1			# No
42543561Skato		movl 0x30(%ebp),%eax		# Get V86 flags
42643561Skato		movw %ax,0x18(%esi)		# Set user flags
42743561Skatointrtn.1:	leal 0x10(%esi),%ebx		# Saved exception frame
42843561Skato		xchgl %edx,%esi 		# Segment registers
42943561Skato		movb $0x4,%cl			# Update seg regs
43043561Skato		rep				#  in INT_V86
43143561Skato		movsl				#  args
432164114Snyanintrtn.2:	xchgl %edx,%esi			# Segment registers
43343561Skato		leal 0x28(%ebp),%edi		# Set up seg
43443561Skato		movb $0x4,%cl			#  regs for
43543561Skato		rep				#  later
43643561Skato		movsl				#  pop
437164114Snyan		xchgl %ebx,%esi			# Restore exception
43843561Skato		movb $0x5,%cl			#  frame to
43943561Skato		rep				#  supervisor
44043561Skato		movsl				#  stack
44143561Skato		movl %esi,MEM_TSS+TSS_ESP1	# Link stack pointer
44243561Skato		popa				# Restore
44343561Skato		leal 0x8(%esp,1),%esp		# Discard err, int no
44443561Skato		popl %es			# Restore
44543561Skato		popl %ds			#  user
44643561Skato		popl %fs			#  segment
44743561Skato		popl %gs			#  registers
44843561Skato		iret				# To user mode
449128710Sru/*
450128710Sru * V86 monitor.
451128710Sru */
45243561Skatov86mon: 	cld				# String ops inc
45343561Skato		pushl $SEL_SDATA		# Set up for
45443561Skato		popl %ds			#  flat addressing
45543561Skato		pusha				# Save registers
45643561Skato		movl %esp,%ebp			# Address stack frame
45743561Skato		movzwl 0x2c(%ebp),%edi		# Load V86 CS
45843561Skato		shll $0x4,%edi			# To linear
45943561Skato		movl 0x28(%ebp),%esi		# Load V86 IP
46043561Skato		addl %edi,%esi			# Code pointer
46143561Skato		xorl %ecx,%ecx			# Zero
46243561Skato		movb $0x2,%cl			# 16-bit operands
46343561Skato		xorl %eax,%eax			# Zero
46443561Skatov86mon.1:	lodsb				# Get opcode
46543561Skato		cmpb $0x66,%al			# Operand size prefix?
46643561Skato		jne v86mon.2			# No
46743561Skato		movb $0x4,%cl			# 32-bit operands
46843561Skato		jmp v86mon.1			# Continue
46943561Skatov86mon.2:	cmpb $0xf4,%al			# HLT?
47043561Skato		jne v86mon.3			# No
47143561Skato		cmpl $inthlt+0x1,%esi		# Is inthlt?
47244368Skato		jne v86mon.7			# No (ignore)
47343561Skato		jmp intrtn			# Return to user mode
47464019Snyanv86mon.3:	cmpb $0xf,%al			# Prefixed instruction?
47564019Snyan		jne v86mon.4			# No
47664019Snyan		cmpb $0x09,(%esi)		# Is it a WBINVD?
47764019Snyan		je v86wbinvd			# Yes
47864019Snyan		cmpb $0x30,(%esi)		# Is it a WRMSR?
47964019Snyan		je v86wrmsr			# Yes
48064019Snyan		cmpb $0x32,(%esi)		# Is it a RDMSR?
48164019Snyan		je v86rdmsr			# Yes
482164114Snyan		cmpb $0x20,(%esi)		# Is this a MOV reg,CRx?
48344368Skato		je v86mov			# Yes
48444368Skatov86mon.4:	cmpb $0xfa,%al			# CLI?
48543561Skato		je v86cli			# Yes
48643561Skato		cmpb $0xfb,%al			# STI?
48743561Skato		je v86sti			# Yes
48843561Skato		movzwl 0x38(%ebp),%ebx		# Load V86 SS
48943561Skato		shll $0x4,%ebx			# To offset
49043561Skato		pushl %ebx			# Save
49143561Skato		addl 0x34(%ebp),%ebx		# Add V86 SP
49243561Skato		movl 0x30(%ebp),%edx		# Load V86 flags
49343561Skato		cmpb $0x9c,%al			# PUSHF/PUSHFD?
49443561Skato		je v86pushf			# Yes
49543561Skato		cmpb $0x9d,%al			# POPF/POPFD?
49643561Skato		je v86popf			# Yes
49743561Skato		cmpb $0xcd,%al			# INT imm8?
49843561Skato		je v86intn			# Yes
49943561Skato		cmpb $0xcf,%al			# IRET/IRETD?
50043561Skato		je v86iret			# Yes
50143561Skato		popl %ebx			# Restore
50243561Skato		popa				# Restore
50343561Skato		jmp except			# Handle exception
50444368Skatov86mon.5:	movl %edx,0x30(%ebp)		# Save V86 flags
50544368Skatov86mon.6:	popl %edx			# V86 SS adjustment
50643561Skato		subl %edx,%ebx			# Save V86
50743561Skato		movl %ebx,0x34(%ebp)		#  SP
50844368Skatov86mon.7:	subl %edi,%esi			# From linear
50943561Skato		movl %esi,0x28(%ebp)		# Save V86 IP
51043561Skato		popa				# Restore
51143561Skato		leal 0x8(%esp,1),%esp		# Discard int no, error
51243561Skato		iret				# To V86 mode
513128710Sru/*
514164114Snyan * Emulate MOV reg,CRx.
515128710Sru */
516164114Snyanv86mov: 	movb 0x1(%esi),%bl		# Fetch Mod R/M byte
517164114Snyan		testb $0x10,%bl			# Read CR2 or CR3?
518164114Snyan		jnz v86mov.1			# Yes
519164114Snyan		movl %cr0,%eax			# Read CR0
520164114Snyan		testb $0x20,%bl			# Read CR4 instead?
521164114Snyan		jz v86mov.2			# No
522164114Snyan		movl %cr4,%eax			# Read CR4
523164114Snyan		jmp v86mov.2
524164114Snyanv86mov.1:	movl %cr2,%eax			# Read CR2
525164114Snyan		testb $0x08,%bl			# Read CR3 instead?
526164114Snyan		jz v86mov.2			# No
527164114Snyan		movl %cr3,%eax			# Read CR3
528164114Snyanv86mov.2:	andl $0x7,%ebx			# Compute offset in
529164114Snyan		shl $2,%ebx			#  frame of destination
530164114Snyan		neg %ebx			#  register
531164114Snyan		movl %eax,0x1c(%ebp,%ebx,1)	# Store CR to reg
53264019Snyan		incl %esi			# Adjust IP
533128710Sru/*
534128710Sru * Return from emulating a 0x0f prefixed instruction
535128710Sru */
53664019Snyanv86preret:	incl %esi			# Adjust IP
53744368Skato		jmp v86mon.7			# Finish up
538128710Sru/*
539128710Sru * Emulate WBINVD
540128710Sru */
54164019Snyanv86wbinvd:	wbinvd				# Write back and invalidate
54264019Snyan						#  cache
54364019Snyan		jmp v86preret			# Finish up
544128710Sru/*
545128710Sru * Emulate WRMSR
546128710Sru */
54764019Snyanv86wrmsr:	movl 0x18(%ebp),%ecx		# Get user's %ecx (MSR to write)
54864019Snyan		movl 0x14(%ebp),%edx		# Load the value
54964019Snyan		movl 0x1c(%ebp),%eax		#  to write
55064019Snyan		wrmsr				# Write MSR
55164019Snyan		jmp v86preret			# Finish up
552128710Sru/*
553128710Sru * Emulate RDMSR
554128710Sru */
55564019Snyanv86rdmsr:	movl 0x18(%ebp),%ecx		# MSR to read
55664019Snyan		rdmsr				# Read the MSR
55764019Snyan		movl %eax,0x1c(%ebp)		# Return the value of
55864019Snyan		movl %edx,0x14(%ebp)		#  the MSR to the user
55964019Snyan		jmp v86preret			# Finish up
560128710Sru/*
561128710Sru * Emulate CLI.
562128710Sru */
56343561Skatov86cli: 	andb $~0x2,0x31(%ebp)		# Clear IF
56444368Skato		jmp v86mon.7			# Finish up
565128710Sru/*
566128710Sru * Emulate STI.
567128710Sru */
56843561Skatov86sti: 	orb $0x2,0x31(%ebp)		# Set IF
56944368Skato		jmp v86mon.7			# Finish up
570128710Sru/*
571128710Sru * Emulate PUSHF/PUSHFD.
572128710Sru */
57343561Skatov86pushf:	subl %ecx,%ebx			# Adjust SP
57443561Skato		cmpb $0x4,%cl			# 32-bit
57543561Skato		je v86pushf.1			# Yes
57661064Snyan		data16				# 16-bit
57743561Skatov86pushf.1:	movl %edx,(%ebx)		# Save flags
57844368Skato		jmp v86mon.6			# Finish up
579128710Sru/*
580128710Sru * Emulate IRET/IRETD.
581128710Sru */
58243561Skatov86iret:	movzwl (%ebx),%esi		# Load V86 IP
58343561Skato		movzwl 0x2(%ebx),%edi		# Load V86 CS
58443561Skato		leal 0x4(%ebx),%ebx		# Adjust SP
58543561Skato		movl %edi,0x2c(%ebp)		# Save V86 CS
58643561Skato		xorl %edi,%edi			# No ESI adjustment
587128710Sru/*
588128710Sru * Emulate POPF/POPFD (and remainder of IRET/IRETD).
589128710Sru */
59043561Skatov86popf:	cmpb $0x4,%cl			# 32-bit?
59143561Skato		je v86popf.1			# Yes
59243561Skato		movl %edx,%eax			# Initialize
59361064Snyan		data16				# 16-bit
59443561Skatov86popf.1:	movl (%ebx),%eax		# Load flags
59543561Skato		addl %ecx,%ebx			# Adjust SP
59643561Skato		andl $V86_FLG,%eax		# Merge
59743561Skato		andl $~V86_FLG,%edx		#  the
59843561Skato		orl %eax,%edx			#  flags
59944368Skato		jmp v86mon.5			# Finish up
600128710Sru/*
601128710Sru * trap int 15, function 87
602128710Sru * reads %es:%si from saved registers on stack to find a GDT containing
603128710Sru * source and destination locations
604128710Sru * reads count of words from saved %cx
605128710Sru * returns success by setting %ah to 0
606128710Sru */
607164114Snyanint15_87:	pushl %esi			# Save
608164114Snyan		pushl %edi			#  registers
609164114Snyan		movl 0x3C(%ebp),%edi		# Load ES
610164114Snyan		movzwl 0x4(%ebp),%eax		# Load user's SI
611164114Snyan		shll $0x4,%edi			# EDI = (ES << 4) +
612164114Snyan		addl %eax,%edi			#   SI
613164114Snyan		movl 0x11(%edi),%eax		# Read base of
614164114Snyan		movb 0x17(%edi),%al		#  GDT entry
615164114Snyan		ror $8,%eax			#  for source
616164114Snyan		xchgl %eax,%esi			#  into %esi
617164114Snyan		movl 0x19(%edi),%eax		# Read base of
618164114Snyan		movb 0x1f(%edi),%al		#  GDT entry for
619164114Snyan		ror $8,%eax			#  destination
620164114Snyan		xchgl %eax,%edi			#  into %edi
62152202Skato		pushl %ds			# Make:
62252202Skato		popl %es			# es = ds
623164114Snyan		movzwl 0x18(%ebp),%ecx		# Get user's CX
624164114Snyan		shll $0x1,%ecx			# Convert count from words
62552202Skato		rep				# repeat...
62652202Skato		movsb				#  perform copy.
627164114Snyan		popl %edi			# Restore
628164114Snyan		popl %esi			#  registers
62952202Skato		movb $0x0,0x1d(%ebp)		# set ah = 0 to indicate
63052202Skato						#  success
63152202Skato		andb $0xfe,%dl			# clear CF
63252202Skato		jmp v86mon.5			# Finish up
63352202Skato
634128710Sru/*
635128710Sru * Reboot the machine by setting the reboot flag and exiting
636128710Sru */
63758871Skatoreboot:		orb $0x1,btx_hdr+0x7		# Set the reboot flag
63858871Skato		jmp exit			# Terminate BTX and reboot
63958871Skato
640128710Sru/*
641128710Sru * Emulate INT imm8... also make sure to check if it's int 15/87
642128710Sru */
64343561Skatov86intn:	lodsb				# Get int no
64458871Skato		cmpb $0x19,%al			# is it int 19?
64558871Skato		je reboot			#  yes, reboot the machine
64652202Skato		cmpb $0x15,%al			# is it int 15?
647164114Snyan		jne v86intn.1			#  no, skip parse
648164114Snyan		cmpb $0x87,0x1d(%ebp)		# is it the memcpy subfunction?
649164114Snyan		je int15_87			#  yes
650164114Snyan		cmpw $0x4f53,0x1c(%ebp)		# is it the delete key callout?
651164114Snyan		jne v86intn.1			#  no, handle the int normally
652164114Snyan		movb BDA_KEYFLAGS,%ch		# get the shift key state
653164114Snyan		andb $0x18,%ch			# mask off just Ctrl and Alt
654164114Snyan		cmpb $0x18,%ch			# are both Ctrl and Alt down?
655164114Snyan		je reboot			# yes, reboot the machine
656164114Snyanv86intn.1:	subl %edi,%esi			# From
65743561Skato		shrl $0x4,%edi			#  linear
65843561Skato		movw %dx,-0x2(%ebx)		# Save flags
65943561Skato		movw %di,-0x4(%ebx)		# Save CS
66043561Skato		leal -0x6(%ebx),%ebx		# Adjust SP
66143561Skato		movw %si,(%ebx) 		# Save IP
66243561Skato		shll $0x2,%eax			# Scale
66343561Skato		movzwl (%eax),%esi		# Load IP
66443561Skato		movzwl 0x2(%eax),%edi		# Load CS
66543561Skato		movl %edi,0x2c(%ebp)		# Save CS
66643561Skato		xorl %edi,%edi			# No ESI adjustment
66761064Snyan		andb $~0x1,%dh			# Clear TF
66844368Skato		jmp v86mon.5			# Finish up
669128710Sru/*
670128710Sru * Hardware interrupt jump table.
671128710Sru */
67261064Snyanintx20: 	push $0x8			# Int 0x20: IRQ0
67343561Skato		jmp int_hw			# V86 int 0x8
67461064Snyan		push $0x9			# Int 0x21: IRQ1
67543561Skato		jmp int_hw			# V86 int 0x9
67661064Snyan		push $0xa			# Int 0x22: IRQ2
67743561Skato		jmp int_hw			# V86 int 0xa
67861064Snyan		push $0xb			# Int 0x23: IRQ3
67943561Skato		jmp int_hw			# V86 int 0xb
68061064Snyan		push $0xc			# Int 0x24: IRQ4
68143561Skato		jmp int_hw			# V86 int 0xc
68261064Snyan		push $0xd			# Int 0x25: IRQ5
68343561Skato		jmp int_hw			# V86 int 0xd
68461064Snyan		push $0xe			# Int 0x26: IRQ6
68543561Skato		jmp int_hw			# V86 int 0xe
68661064Snyan		push $0xf			# Int 0x27: IRQ7
68743561Skato		jmp int_hw			# V86 int 0xf
68861064Snyan		push $0x10			# Int 0x28: IRQ8
68943561Skato		jmp int_hw			# V86 int 0x10
69061064Snyan		push $0x11			# Int 0x29: IRQ9
69143561Skato		jmp int_hw			# V86 int 0x11
69261064Snyan		push $0x12			# Int 0x2a: IRQ10
69343561Skato		jmp int_hw			# V86 int 0x12
69461064Snyan		push $0x13			# Int 0x2b: IRQ11
69543561Skato		jmp int_hw			# V86 int 0x13
69661064Snyan		push $0x14			# Int 0x2c: IRQ12
69743561Skato		jmp int_hw			# V86 int 0x14
69861064Snyan		push $0x15			# Int 0x2d: IRQ13
69943561Skato		jmp int_hw			# V86 int 0x15
70061064Snyan		push $0x16			# Int 0x2e: IRQ14
70143561Skato		jmp int_hw			# V86 int 0x16
70261064Snyan		push $0x17			# Int 0x2f: IRQ15
70343561Skato		jmp int_hw			# V86 int 0x17
704128710Sru/*
705128710Sru * Reflect hardware interrupts.
706128710Sru */
70743561Skatoint_hw: 	testb $0x2,0xe(%esp,1)		# V86 mode?
70843561Skato		jz intusr			# No
70943561Skato		pushl $SEL_SDATA		# Address
71043561Skato		popl %ds			#  data
71143561Skato		xchgl %eax,(%esp,1)		# Swap EAX, int no
71243561Skato		pushl %ebp			# Address
71343561Skato		movl %esp,%ebp			#  stack frame
71443561Skato		pushl %ebx			# Save
71543561Skato		shll $0x2,%eax			# Get int
71643561Skato		movl (%eax),%eax		#  vector
71743561Skato		subl $0x6,0x14(%ebp)		# Adjust V86 ESP
71843561Skato		movzwl 0x18(%ebp),%ebx		# V86 SS
71943561Skato		shll $0x4,%ebx			#  * 0x10
72043561Skato		addl 0x14(%ebp),%ebx		#  + V86 ESP
72143561Skato		xchgw %ax,0x8(%ebp)		# Swap V86 IP
72243561Skato		rorl $0x10,%eax 		# Swap words
72343561Skato		xchgw %ax,0xc(%ebp)		# Swap V86 CS
72443561Skato		roll $0x10,%eax 		# Swap words
72543561Skato		movl %eax,(%ebx)		# CS:IP for IRET
72643561Skato		movl 0x10(%ebp),%eax		# V86 flags
72743561Skato		movw %ax,0x4(%ebx)		# Flags for IRET
72843561Skato		andb $~0x3,0x11(%ebp)		# Clear IF, TF
72943561Skato		popl %ebx			# Restore
73043561Skato		popl %ebp			#  saved
73143561Skato		popl %eax			#  registers
73243561Skato		iret				# To V86 mode
733128710Sru/*
734128710Sru * Invoke V86 interrupt from user mode, with arguments.
735128710Sru */
73643561Skatointx31: 	stc				# Have btx_v86
73743561Skato		pushl %eax			# Missing int no
738128710Sru/*
739128710Sru * Invoke V86 interrupt from user mode.
740128710Sru */
74143561Skatointusr: 	std				# String ops dec
74243561Skato		pushl %eax			# Expand
74343561Skato		pushl %eax			#  stack
74443561Skato		pushl %eax			#  frame
74543561Skato		pusha				# Save
74643561Skato		pushl %gs			# Save
74743561Skato		movl %esp,%eax			#  seg regs
74843561Skato		pushl %fs			#  and
74943561Skato		pushl %ds			#  point
75043561Skato		pushl %es			#  to them
75161064Snyan		push $SEL_SDATA			# Set up
75243561Skato		popl %ds			#  to
75343561Skato		pushl %ds			#  address
75443561Skato		popl %es			#  data
75543561Skato		movl $MEM_USR,%ebx		# User base
75643561Skato		movl %ebx,%edx			#  address
75743561Skato		jc intusr.1			# If btx_v86
75843561Skato		xorl %edx,%edx			# Control flags
75943561Skato		xorl %ebp,%ebp			# btx_v86 pointer
76043561Skatointusr.1:	leal 0x50(%esp,1),%esi		# Base of frame
76143561Skato		pushl %esi			# Save
76243561Skato		addl -0x4(%esi),%ebx		# User ESP
76343561Skato		movl MEM_TSS+TSS_ESP1,%edi	# Link stack pointer
76443561Skato		leal -0x4(%edi),%edi		# Adjust for push
76543561Skato		xorl %ecx,%ecx			# Zero
76643561Skato		movb $0x5,%cl			# Push exception
76743561Skato		rep				#  frame on
76843561Skato		movsl				#  link stack
76943561Skato		xchgl %eax,%esi 		# Saved seg regs
77043561Skato		movl 0x40(%esp,1),%eax		# Get int no
77143561Skato		testl %edx,%edx 		# Have btx_v86?
77243561Skato		jz intusr.2			# No
77343561Skato		movl (%ebx),%ebp		# btx_v86 pointer
77443561Skato		movb $0x4,%cl			# Count
77543561Skato		addl %ecx,%ebx			# Adjust for pop
77643561Skato		rep				# Push saved seg regs
77743561Skato		movsl				#  on link stack
77843561Skato		addl %ebp,%edx			# Flatten btx_v86 ptr
77943561Skato		leal 0x14(%edx),%esi		# Seg regs pointer
78043561Skato		movl 0x4(%edx),%eax		# Get int no/address
78143561Skato		movzwl 0x2(%edx),%edx		# Get control flags
78243561Skatointusr.2:	movl %ebp,(%edi)		# Push btx_v86 and
78343561Skato		movl %edi,MEM_TSS+TSS_ESP1	#  save link stack ptr
78443561Skato		popl %edi			# Base of frame
78543561Skato		xchgl %eax,%ebp 		# Save intno/address
78643561Skato		movl 0x48(%esp,1),%eax		# Get flags
78743561Skato		testb $0x2,%dl			# Simulate CALLF?
78843561Skato		jnz intusr.3			# Yes
78943561Skato		decl %ebx			# Push flags
79043561Skato		decl %ebx			#  on V86
79143561Skato		movw %ax,(%ebx) 		#  stack
79243561Skatointusr.3:	movb $0x4,%cl			# Count
79343561Skato		subl %ecx,%ebx			# Push return address
79443561Skato		movl $inthlt,(%ebx)		#  on V86 stack
79543561Skato		rep				# Copy seg regs to
79643561Skato		movsl				#  exception frame
79743561Skato		xchgl %eax,%ecx 		# Save flags
79843561Skato		movl %ebx,%eax			# User ESP
79943561Skato		subl $V86_STK,%eax		# Less bytes
80043561Skato		ja intusr.4			#  to
80143561Skato		xorl %eax,%eax			#  keep
80243561Skatointusr.4:	shrl $0x4,%eax			# Gives segment
80343561Skato		stosl				# Set SS
80443561Skato		shll $0x4,%eax			# To bytes
80543561Skato		xchgl %eax,%ebx 		# Swap
80643561Skato		subl %ebx,%eax			# Gives offset
80743561Skato		stosl				# Set ESP
80843561Skato		xchgl %eax,%ecx 		# Get flags
80943561Skato		btsl $0x11,%eax 		# Set VM
81061064Snyan		andb $~0x1,%ah			# Clear TF
81143561Skato		stosl				# Set EFL
81243561Skato		xchgl %eax,%ebp 		# Get int no/address
81343561Skato		testb $0x1,%dl			# Address?
81443561Skato		jnz intusr.5			# Yes
81543561Skato		shll $0x2,%eax			# Scale
81643561Skato		movl (%eax),%eax		# Load int vector
81743561Skatointusr.5:	movl %eax,%ecx			# Save
81843561Skato		shrl $0x10,%eax 		# Gives segment
81943561Skato		stosl				# Set CS
82043561Skato		movw %cx,%ax			# Restore
82143561Skato		stosl				# Set EIP
82243561Skato		leal 0x10(%esp,1),%esp		# Discard seg regs
82343561Skato		popa				# Restore
82443561Skato		iret				# To V86 mode
825128710Sru/*
826128710Sru * System Call.
827128710Sru */
82843561Skatointx30: 	cmpl $SYS_EXEC,%eax		# Exec system call?
82943561Skato		jne intx30.1			# No
83043561Skato		pushl %ss			# Set up
83143561Skato		popl %es			#  all
83243561Skato		pushl %es			#  segment
83343561Skato		popl %ds			#  registers
83443561Skato		pushl %ds			#  for the
83543561Skato		popl %fs			#  program
83643561Skato		pushl %fs			#  we're
83743561Skato		popl %gs			#  invoking
83843561Skato		movl $MEM_USR,%eax		# User base address
83943561Skato		addl 0xc(%esp,1),%eax		# Change to user
84043561Skato		leal 0x4(%eax),%esp		#  stack
84143561Skato		popl %eax			# Call
84243561Skato		call *%eax			#  program
84378650Skatointx30.1:	orb $0x1,%ss:btx_hdr+0x7	# Flag reboot
84443561Skato		jmp exit			# Exit
845128710Sru/*
846128710Sru * Dump structure [EBX] to [EDI], using format string [ESI].
847128710Sru */
84843561Skatodump.0: 	stosb				# Save char
84943561Skatodump:		lodsb				# Load char
85043561Skato		testb %al,%al			# End of string?
85143561Skato		jz dump.10			# Yes
85243561Skato		testb $0x80,%al 		# Control?
85343561Skato		jz dump.0			# No
85443561Skato		movb %al,%ch			# Save control
85543561Skato		movb $'=',%al			# Append
85643561Skato		stosb				#  '='
85743561Skato		lodsb				# Get offset
85843561Skato		pushl %esi			# Save
85943561Skato		movsbl %al,%esi 		# To
86043561Skato		addl %ebx,%esi			#  pointer
86143561Skato		testb $DMP_X16,%ch		# Dump word?
86243561Skato		jz dump.1			# No
86343561Skato		lodsw				# Get and
86443561Skato		call hex16			#  dump it
86543561Skatodump.1: 	testb $DMP_X32,%ch		# Dump long?
86643561Skato		jz dump.2			# No
86743561Skato		lodsl				# Get and
86843561Skato		call hex32			#  dump it
86943561Skatodump.2: 	testb $DMP_MEM,%ch		# Dump memory?
87043561Skato		jz dump.8			# No
87143561Skato		pushl %ds			# Save
87243561Skato		testb $0x2,0x52(%ebx)		# V86 mode?
87343561Skato		jnz dump.3			# Yes
87461064Snyan		verr 0x4(%esi)	 		# Readable selector?
87543561Skato		jnz dump.3			# No
87643561Skato		ldsl (%esi),%esi		# Load pointer
87743561Skato		jmp dump.4			# Join common code
87843561Skatodump.3: 	lodsl				# Set offset
87943561Skato		xchgl %eax,%edx 		# Save
88043561Skato		lodsl				# Get segment
88143561Skato		shll $0x4,%eax			#  * 0x10
88243561Skato		addl %edx,%eax			#  + offset
88343561Skato		xchgl %eax,%esi 		# Set pointer
88486497Snyandump.4: 	movb $2,%dl			# Num lines
88586497Snyandump.4a:	movb $0x10,%cl			# Bytes to dump
88643561Skatodump.5: 	lodsb				# Get byte and
88743561Skato		call hex8			#  dump it
88843561Skato		decb %cl			# Keep count
88986497Snyan		jz dump.6a			# If done
89043561Skato		movb $'-',%al			# Separator
89143561Skato		cmpb $0x8,%cl			# Half way?
89243561Skato		je dump.6			# Yes
89343561Skato		movb $' ',%al			# Use space
89443561Skatodump.6: 	stosb				# Save separator
89543561Skato		jmp dump.5			# Continue
89686497Snyandump.6a:	decb %dl			# Keep count
89786497Snyan		jz dump.7			# If done
89886497Snyan		movb $0xa,%al			# Line feed
89986497Snyan		stosb				# Save one
90086497Snyan		movb $7,%cl			# Leading
90186497Snyan		movb $' ',%al			#  spaces
90286497Snyandump.6b:	stosb				# Dump
90386497Snyan		decb %cl			#  spaces
90486497Snyan		jnz dump.6b
90586497Snyan		jmp dump.4a			# Next line
90643561Skatodump.7: 	popl %ds			# Restore
90743561Skatodump.8: 	popl %esi			# Restore
90843561Skato		movb $0xa,%al			# Line feed
90943561Skato		testb $DMP_EOL,%ch		# End of line?
91043561Skato		jnz dump.9			# Yes
91143561Skato		movb $' ',%al			# Use spaces
91243561Skato		stosb				# Save one
91343561Skatodump.9: 	jmp dump.0			# Continue
91443561Skatodump.10:	stosb				# Terminate string
91543561Skato		ret				# To caller
916128710Sru/*
917128710Sru * Convert EAX, AX, or AL to hex, saving the result to [EDI].
918128710Sru */
91943561Skatohex32:		pushl %eax			# Save
92043561Skato		shrl $0x10,%eax 		# Do upper
92143561Skato		call hex16			#  16
92243561Skato		popl %eax			# Restore
92343561Skatohex16:		call hex16.1			# Do upper 8
92443561Skatohex16.1:	xchgb %ah,%al			# Save/restore
92543561Skatohex8:		pushl %eax			# Save
92643561Skato		shrb $0x4,%al			# Do upper
92743561Skato		call hex8.1			#  4
92843561Skato		popl %eax			# Restore
92943561Skatohex8.1: 	andb $0xf,%al			# Get lower 4
93043561Skato		cmpb $0xa,%al			# Convert
93143561Skato		sbbb $0x69,%al			#  to hex
93243561Skato		das				#  digit
93343561Skato		orb $0x20,%al			# To lower case
93443561Skato		stosb				# Save char
93543561Skato		ret				# (Recursive)
936128710Sru/*
937128710Sru * Output zero-terminated string [ESI] to the console.
938128710Sru */
93943561Skatoputstr.0:	call putchr			# Output char
94043561Skatoputstr: 	lodsb				# Load char
94143561Skato		testb %al,%al			# End of string?
94243561Skato		jnz putstr.0			# No
94343561Skato		ret				# To caller
944125780Snyan#ifdef BTX_SERIAL
94586497Snyan		.set SIO_PRT,SIOPRT		# Base port
94686497Snyan		.set SIO_FMT,SIOFMT		# 8N1
94786497Snyan		.set SIO_DIV,(115200/SIOSPD)	# 115200 / SPD
94886497Snyan
949138189Snyan/*
950128710Sru * void sio_init(void)
951138189Snyan */
95286497Snyansio_init:	movw $SIO_PRT+0x3,%dx		# Data format reg
95386497Snyan		movb $SIO_FMT|0x80,%al		# Set format
95486497Snyan		outb %al,(%dx)			#  and DLAB
95586497Snyan		pushl %edx			# Save
95686497Snyan		subb $0x3,%dl			# Divisor latch reg
95786497Snyan		movw $SIO_DIV,%ax		# Set
95886497Snyan		outw %ax,(%dx)			#  BPS
95986497Snyan		popl %edx			# Restore
96086497Snyan		movb $SIO_FMT,%al		# Clear
96186497Snyan		outb %al,(%dx)			#  DLAB
96286497Snyan		incl %edx			# Modem control reg
96386497Snyan		movb $0x3,%al			# Set RTS,
96486497Snyan		outb %al,(%dx)			#  DTR
96586497Snyan		incl %edx			# Line status reg
96686497Snyan
967138189Snyan/*
968128710Sru * void sio_flush(void)
969138189Snyan */
97086497Snyansio_flush.0:	call sio_getc.1 		# Get character
97186497Snyansio_flush:	call sio_ischar 		# Check for character
97286497Snyan		jnz sio_flush.0 		# Till none
97386497Snyan		ret				# To caller
97486497Snyan
975138189Snyan/*
976128710Sru * void sio_putc(int c)
977138189Snyan */
97886497Snyansio_putc:	movw $SIO_PRT+0x5,%dx		# Line status reg
97986497Snyan		xor %ecx,%ecx			# Timeout
98086497Snyan		movb $0x40,%ch			#  counter
98186497Snyansio_putc.1:	inb (%dx),%al			# Transmitter
98286497Snyan		testb $0x20,%al 		#  buffer empty?
98386497Snyan		loopz sio_putc.1		# No
98486497Snyan		jz sio_putc.2			# If timeout
98586497Snyan		movb 0x4(%esp,1),%al		# Get character
98686497Snyan		subb $0x5,%dl			# Transmitter hold reg
98786497Snyan		outb %al,(%dx)			# Write character
98886497Snyansio_putc.2:	ret $0x4			# To caller
98986497Snyan
990138189Snyan/*
991128710Sru * int sio_getc(void)
992138189Snyan */
99386497Snyansio_getc:	call sio_ischar 		# Character available?
99486497Snyan		jz sio_getc			# No
99586497Snyansio_getc.1:	subb $0x5,%dl			# Receiver buffer reg
99686497Snyan		inb (%dx),%al			# Read character
99786497Snyan		ret				# To caller
99886497Snyan
999138189Snyan/*
1000128710Sru * int sio_ischar(void)
1001138189Snyan */
100286497Snyansio_ischar:	movw $SIO_PRT+0x5,%dx		# Line status register
100386497Snyan		xorl %eax,%eax			# Zero
100486497Snyan		inb (%dx),%al			# Received data
100586497Snyan		andb $0x1,%al			#  ready?
100686497Snyan		ret				# To caller
100786497Snyan
1008128710Sru/*
1009128710Sru * Output character AL to the serial console.
1010128710Sru */
101186497Snyanputchr: 	pusha				# Save
101286497Snyan		cmpb $10, %al			# is it a newline?
101386497Snyan		jne putchr.1			#  no?, then leave
101486497Snyan		push $13			# output a carriage
101586497Snyan		call sio_putc			#  return first
101686497Snyan		movb $10, %al			# restore %al
101786497Snyanputchr.1:	pushl %eax			# Push the character
101886497Snyan						#  onto the stack
101986497Snyan		call sio_putc			# Output the character
102086497Snyan		popa				# Restore
102186497Snyan		ret				# To caller
1022125780Snyan#else
1023128710Sru/*
1024128710Sru * Output character AL to the console.
1025128710Sru */
102643561Skatoputchr: 	pusha				# Save
102743561Skato		xorl %ecx,%ecx			# Zero for loops
102843561Skato		movb $SCR_MAT,%ah		# Mode/attribute
102943561Skato		movl $BDA_POS,%ebx		# BDA pointer
103043561Skato		movw (%ebx),%dx 		# Cursor position
103143561Skato		movl $0xa0000,%edi
103243561Skatoputchr.1:	cmpb $0xa,%al			# New line?
103343561Skato		je putchr.2			# Yes
103443561Skato		movw %dx,%cx
103543561Skato		movb %al,(%edi,%ecx,1)		# Write char
103643561Skato		addl $0x2000,%ecx
103743561Skato		movb %ah,(%edi,%ecx,1)		# Write attr
103843561Skato		addw $0x02,%dx
103943561Skato		jmp putchr.3
104043561Skatoputchr.2:	movw %dx,%ax
104143561Skato		movb $SCR_COL*2,%dl
104243561Skato		div %dl
104343561Skato		incb %al
104443561Skato		mul %dl
104543561Skato		movw %ax,%dx
104643561Skatoputchr.3:	cmpw $SCR_ROW*SCR_COL*2,%dx
104743561Skato		jb putchr.4			# No
104843561Skato		leal 2*SCR_COL(%edi),%esi	# New top line
104943561Skato		movw $(SCR_ROW-1)*SCR_COL/2,%cx # Words to move
105043561Skato		rep				# Scroll
105143561Skato		movsl				#  screen
105286497Snyan		movb $0x20,%al			# Space
105343561Skato		xorb %ah,%ah
105443561Skato		movb $SCR_COL,%cl		# Columns to clear
105543561Skato		rep				# Clear
105643561Skato		stosw				#  line
105743561Skato		movw $(SCR_ROW-1)*SCR_COL*2,%dx
105843561Skatoputchr.4:	movw %dx,(%ebx) 		# Update position
105943561Skato		popa				# Restore
106043561Skato		ret				# To caller
1061125780Snyan#endif
106243561Skato
106343561Skato		.p2align 4
1064128710Sru/*
1065128710Sru * Global descriptor table.
1066128710Sru */
106743561Skatogdt:		.word 0x0,0x0,0x0,0x0		# Null entry
106843561Skato		.word 0xffff,0x0,0x9a00,0xcf	# SEL_SCODE
106943561Skato		.word 0xffff,0x0,0x9200,0xcf	# SEL_SDATA
107043561Skato		.word 0xffff,0x0,0x9a00,0x0	# SEL_RCODE
107143561Skato		.word 0xffff,0x0,0x9200,0x0	# SEL_RDATA
107243561Skato		.word 0xffff,MEM_USR,0xfa00,0xcf# SEL_UCODE
107343561Skato		.word 0xffff,MEM_USR,0xf200,0xcf# SEL_UDATA
107443561Skato		.word _TSSLM,MEM_TSS,0x8900,0x0 # SEL_TSS
107543561Skatogdt.1:
1076128710Sru/*
1077128710Sru * Pseudo-descriptors.
1078128710Sru */
107943561Skatogdtdesc:	.word gdt.1-gdt-1,gdt,0x0	# GDT
108043561Skatoidtdesc:	.word _IDTLM,MEM_IDT,0x0	# IDT
108143561Skatoivtdesc:	.word 0x400-0x0-1,0x0,0x0	# IVT
1082128710Sru/*
1083128710Sru * IDT construction control string.
1084128710Sru */
108543561Skatoidtctl: 	.byte 0x10,  0x8e		# Int 0x0-0xf
108643561Skato		.word 0x7dfb,intx00		#  (exceptions)
108743561Skato		.byte 0x10,  0x8e		# Int 0x10
108843561Skato		.word 0x1,   intx10		#  (exception)
108943561Skato		.byte 0x10,  0x8e		# Int 0x20-0x2f
109043561Skato		.word 0xffff,intx20		#  (hardware)
109143561Skato		.byte 0x1,   0xee		# int 0x30
109243561Skato		.word 0x1,   intx30		#  (system call)
109343561Skato		.byte 0x2,   0xee		# Int 0x31-0x32
109443561Skato		.word 0x1,   intx31		#  (V86, null)
109543561Skato		.byte 0x0			# End of string
1096128710Sru/*
1097128710Sru * Dump format string.
1098128710Sru */
109943561Skatodmpfmt: 	.byte '\n'			# "\n"
110043561Skato		.ascii "int"			# "int="
110143561Skato		.byte 0x80|DMP_X32,	   0x40 # "00000000  "
110243561Skato		.ascii "err"			# "err="
110343561Skato		.byte 0x80|DMP_X32,	   0x44 # "00000000  "
110443561Skato		.ascii "efl"			# "efl="
110543561Skato		.byte 0x80|DMP_X32,	   0x50 # "00000000  "
110643561Skato		.ascii "eip"			# "eip="
110743561Skato		.byte 0x80|DMP_X32|DMP_EOL,0x48 # "00000000\n"
110843561Skato		.ascii "eax"			# "eax="
110943561Skato		.byte 0x80|DMP_X32,	   0x34 # "00000000  "
111043561Skato		.ascii "ebx"			# "ebx="
111143561Skato		.byte 0x80|DMP_X32,	   0x28 # "00000000  "
111243561Skato		.ascii "ecx"			# "ecx="
111343561Skato		.byte 0x80|DMP_X32,	   0x30 # "00000000  "
111443561Skato		.ascii "edx"			# "edx="
111543561Skato		.byte 0x80|DMP_X32|DMP_EOL,0x2c # "00000000\n"
111643561Skato		.ascii "esi"			# "esi="
111743561Skato		.byte 0x80|DMP_X32,	   0x1c # "00000000  "
111843561Skato		.ascii "edi"			# "edi="
111943561Skato		.byte 0x80|DMP_X32,	   0x18 # "00000000  "
112043561Skato		.ascii "ebp"			# "ebp="
112143561Skato		.byte 0x80|DMP_X32,	   0x20 # "00000000  "
112243561Skato		.ascii "esp"			# "esp="
112343561Skato		.byte 0x80|DMP_X32|DMP_EOL,0x0	# "00000000\n"
112443561Skato		.ascii "cs"			# "cs="
112543561Skato		.byte 0x80|DMP_X16,	   0x4c # "0000  "
112643561Skato		.ascii "ds"			# "ds="
112743561Skato		.byte 0x80|DMP_X16,	   0xc	# "0000  "
112843561Skato		.ascii "es"			# "es="
112943561Skato		.byte 0x80|DMP_X16,	   0x8	# "0000  "
113043561Skato		.ascii "  "			# "  "
113143561Skato		.ascii "fs"			# "fs="
113243561Skato		.byte 0x80|DMP_X16,	   0x10 # "0000  "
113343561Skato		.ascii "gs"			# "gs="
113443561Skato		.byte 0x80|DMP_X16,	   0x14 # "0000  "
113543561Skato		.ascii "ss"			# "ss="
113643561Skato		.byte 0x80|DMP_X16|DMP_EOL,0x4	# "0000\n"
113743561Skato		.ascii "cs:eip" 		# "cs:eip="
113843561Skato		.byte 0x80|DMP_MEM|DMP_EOL,0x48 # "00 00 ... 00 00\n"
113943561Skato		.ascii "ss:esp" 		# "ss:esp="
114043561Skato		.byte 0x80|DMP_MEM|DMP_EOL,0x0	# "00 00 ... 00 00\n"
114186497Snyan		.asciz "BTX halted\n"		# End
1142128710Sru/*
1143128710Sru * End of BTX memory.
1144128710Sru */
114543561Skato		.p2align 4
114643561Skatobreak:
1147