8000 minix/boot/doshead.s at v3.1.3b · ca-d/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
executable file
·
1369 lines (1259 loc) · 34.4 KB

doshead.s

File metadata and controls

executable file
·
1369 lines (1259 loc) · 34.4 KB
pop cx ! Restore count
add 4(bp), cx
adc 6(bp), 0 ! srcaddr += copycount
add 8(bp), cx
adc 10(bp), 0 ! dstaddr += copycount
sub 12(bp), cx
sbb 14(bp), 0 ! count -= copycount
jmp copy ! and repeat
copydone:
pop di
pop si ! Restore C variable registers
pop bp
ret
! u16_t get_word(u32_t addr);
! void put_word(u32_t addr, u16_t word);
! Read or write a 16 bits word at an arbitrary location.
.define _get_word, _put_word
_get_word:
mov bx, sp
call gp_getaddr
mov ax, (bx) ! Word to get from addr
jmp gp_ret
_put_word:
mov bx, sp
push 6(bx) ! Word to store at addr
call gp_getaddr
pop (bx) ! Store the word
jmp gp_ret
gp_getaddr:
mov ax, 2(bx)
mov dx, 4(bx)
call abs2seg
mov bx, ax
mov ds, dx ! ds:bx = addr
ret
gp_ret:
push es
pop ds ! Restore ds
ret
! void relocate(void);
! After the program has copied itself to a safer place, it needs to change
! the segment registers. Caddr has already been set to the new location.
.define _relocate
_relocate:
pop bx ! Return address
mov ax, (_caddr+0)
mov dx, (_caddr+2)
call abs2seg
mov cx, dx ! cx = new code segment
mov ax, cs ! Old code segment
sub ax, cx ! ax = -(new - old) = -Moving offset
mov dx, ds
sub dx, ax
mov ds, dx ! ds += (new - old)
mov es, dx
mov ss, dx
xor ax, ax
call seg2abs
mov (_daddr+0), ax
mov (_daddr+2), dx ! New data address
push cx ! New text segment
push bx ! Return offset of this function
retf ! Relocate
! void *brk(void *addr)
! void *sbrk(size_t incr)
! Cannot fail implementations of brk(2) and sbrk(3), so we can use
! malloc(3). They reboot on stack collision instead of returning -1.
.sect .data
.align 2
break: .data2 _end ! A fake heap pointer
.sect .text
.define _brk, __brk, _sbrk, __sbrk
_brk:
__brk: ! __brk is for the standard C compiler
xor ax, ax
jmp sbrk ! break= 0; return sbrk(addr);
_sbrk:
__sbrk:
mov ax, (break) ! ax= current break
sbrk: push ax ! save it as future return value
mov bx, sp ! Stack is now: (retval, retaddr, incr, ...)
add ax, 4(bx) ! ax= break + increment
mov (break), ax ! Set new break
lea dx, -1024(bx) ! sp minus a bit of breathing space
cmp dx, ax ! Compare with the new break
jb heaperr ! Suffocating noises
pop ax ! Return old break (0 for brk)
ret
heaperr:push nomem
call _printf
call quit
.sect .rom
nomem: .ascii "\nOut of memory\n\0"
.sect .text
! int dev_open(void);
! Open file 'vdisk' to use as the Minix virtual disk. Store handle in
! vfd. Returns 0 for success, otherwise the DOS error code.
.define _dev_open
_dev_open:
call _dev_close ! If already open then first close
mov dx, (_vdisk) ! ds:dx = Address of file name
mov ax, 0x3D22 ! Open file read-write & deny write
int 0x21
jnc opok ! Open succeeded?
cmp ax, 5 ! Open failed, "access denied"?
jne opbad
mov ax, 0x3D40 ! Open file read-only
int 0x21
jc opbad
opok: mov (vfd), ax ! File handle to open file
xor ax, ax ! Zero for success
opbad: ret
! int dev_close(void);
! Close the dos virtual disk.
.define _dev_close
_dev_close:
mov bx, -1
cmp (vfd), bx ! Already closed?
je 1f
movb ah, 0x3E ! Close file
xchg bx, (vfd) ! bx = vfd; vfd = -1;
int 0x21
jc 0f
1: xor ax, ax
0: ret
! int dev_boundary(u32_t sector);
! Returns false; files have no visible boundaries.
.define _dev_boundary
_dev_boundary:
xor ax, ax
ret
! int readsectors(u32_t bufaddr, u32_t sector, u8_t count)
! int writesectors(u32_t bufaddr, u32_t sector, u8_t count)
! Read/write several sectors from/to the Minix virtual disk. Count
! must fit in a byte. The external variable vfd is the file handle.
! Returns 0 for success, otherwise the DOS error code.
!
.define _readsectors, _writesectors
_writesectors:
push bp
mov bp, sp
movb 13(bp), 0x40 ! Code for a file write
jmp rwsec
_readsectors:
push bp
mov bp, sp
movb 13(bp), 0x3F ! Code for a file read
rwsec:
cmp (vfd), -1 ! Currently closed?
jne 0f
call _dev_open ! Open file if needed
test ax, ax
jnz rwerr
0: mov dx, 8(bp)
mov bx, 10(bp) ! bx-dx = Sector number
mov cx, 9
mul512: shl dx, 1
rcl bx, 1 ! bx-dx *= 512
loop mul512
mov cx, bx ! cx-dx = Byte position in file
mov bx, (vfd) ! bx = File handle
mov ax, 0x4200 ! Lseek absolute
int 0x21
jb rwerr
mov bx, (vfd) ! bx = File handle
mov ax, 4(bp)
mov dx, 6(bp) ! dx-ax = Address to transfer data to/from
call abs2seg
mov ds, dx
mov dx, ax ! ds:dx = Address to transfer data to/from
xorb cl, cl
movb ch, 12(bp) ! ch = Number of sectors to transfer
shl cx, 1 ! cx = Number of bytes to transfer
push cx ! Save count
movb ah, 13(bp) ! Read or write
int 0x21
pop cx ! Restore count
push es
pop ds ! Restore ds
jb rwerr
cmp ax, cx ! All bytes transferred?
je rwall
mov ax, 0x05 ! The DOS code for "I/O error", but different
jmp rwerr
rwall: call wheel ! Display tricks
xor ax, ax
rwerr: pop bp
ret
! int getch(void);
! Read a character from the keyboard, and check for an expired timer.
! A carriage return is changed into a linefeed for UNIX compatibility.
.define _getch
_getch:
xor ax, ax
xchg ax, (unchar) ! Ungotten character?
test ax, ax
jnz gotch
getch: hlt ! Play dead until interrupted (see pause())
movb ah, 0x01 ! Keyboard status
int 0x16
jnz press ! Keypress?
call _expired ! Timer expired?
test ax, ax
jz getch
mov ax, ESC ! Return ESC
ret
press:
xorb ah, ah ! Read character from keyboard
int 0x16
cmpb al, 0x0D ! Carriage return?
jnz nocr
movb al, 0x0A ! Change to linefeed
nocr: cmpb al, ESC ! Escape typed?
jne noesc
inc (escape) ! Set flag
noesc: xorb ah, ah ! ax = al
gotch: ret
! int ungetch(void);
! Return a character to undo a getch().
.define _ungetch
_ungetch:
mov bx, sp
mov ax, 2(bx)
mov (unchar), ax
ret
! int escape(void);
! True if ESC has been typed.
.define _escape
_escape:
movb ah, 0x01 ! Keyboard status
int 0x16
jz escflg ! Keypress?
cmpb al, ESC ! Escape typed?
jne escflg
xorb ah, ah ! Discard the escape
int 0x16
inc (escape) ! Set flag
escflg: xor ax, ax
xchg ax, (escape) ! Escape typed flag
ret
! int putch(int c);
! Write a character in teletype mode. The putk synonym is
! for the kernel printf function that uses it.
! Newlines are automatically preceded by a carriage return.
!
.define _putch, _putk
_putch:
_putk: mov bx, sp
movb al, 2(bx) ! al = character to be printed
testb al, al ! Kernel printf adds a null char to flush queue
jz nulch
cmpb al, 0x0A ! al = newline?
jnz putc
movb al, 0x20 ! Erase w 10BC8 heel and do a carriage return
call plotc ! plotc(' ');
nodirt: movb al, 0x0D
call putc ! putc('\r')
movb al, 0x0A ! Restore the '\n' and print it
putc: movb ah, 0x0E ! Print character in teletype mode
mov bx, 0x0001 ! Page 0, foreground color
int 0x10 ! Call BIOS VIDEO_IO
nulch: ret
! |/-\|/-\|/-\|/-\|/-\ (playtime)
wheel: mov bx, (gp)
movb al, (bx)
inc bx ! al = *gp++;
cmp bx, glyphs+4
jne 0f
mov bx, glyphs
0: mov (gp), bx ! gp= gp == glyphs + 4 ? glyphs : gp;
!jmp plotc
plotc: movb ah, 0x0A ! 0x0A = write character at cursor
mov bx, 0x0001 ! Page 0, foreground color
mov cx, 0x0001 ! Just one character
int 0x10
ret
.sect .data
.align 2
gp: .data2 glyphs
glyphs: .ascii "|/-\\"
.sect .text
! void pause(void);
! Wait for an interrupt using the HLT instruction. This either saves
! power, or tells an x86 emulator that nothing is happening right now.
.define _pause
_pause:
hlt
ret
! void set_mode(unsigned mode);
! void clear_screen(void);
! Set video mode / clear the screen.
.define _set_mode, _clear_screen
_set_mode:
mov bx, sp
mov ax, 2(bx) ! Video mode
cmp ax, (cur_vid_mode)
je modeok ! Mode already as requested?
mov (cur_vid_mode), ax
_clear_screen:
mov ax, (cur_vid_mode)
andb ah, 0x7F ! Test bits 8-14, clear bit 15 (8x8 flag)
jnz xvesa ! VESA extended mode?
int 0x10 ! Reset video (ah = 0)
jmp mdset
xvesa: mov bx, ax ! bx = extended mode
mov ax, 0x4F02 ! Reset video
int 0x10
mdset: testb (cur_vid_mode+1), 0x80
jz setcur ! 8x8 font requested?
mov ax, 0x1112 ! Load ROM 8 by 8 double-dot patterns
xorb bl, bl ! Load block 0
int 0x10
setcur: xor dx, dx ! dl = column = 0, dh = row = 0
xorb bh, bh ! Page 0
movb ah, 0x02 ! Set cursor position
int 0x10
modeok: ret
restore_video: ! To restore the video mode on exit
movb al, 0x20
call plotc ! Erase wheel
push (old_vid_mode)
call _set_mode
pop ax
ret
! u32_t get_tick(void);
! Return the current value of the clock tick counter. This counter
! increments 18.2 times per second. Poll it to do delays. Does not
! work on the original PC, but works on the PC/XT.
.define _get_tick
_get_tick:
xorb ah, ah ! Code for get tick count
int 0x1A
mov ax, dx
mov dx, cx ! dx:ax = cx:dx = tick count
ret
! Functions used to obtain info about the hardware. Boot uses this information
! itself, but will also pass them on to a pure 386 kernel, because one can't
! make BIOS calls from protected mode. The video type could probably be
! determined by the kernel too by looking at the hardware, but there is a small
! chance on errors that the monitor allows you to correct by setting variables.
.define _get_bus ! returns type of system bus
.define _get_video ! returns type of display
! u16_t get_bus(void)
! Return type of system bus, in order: XT, AT, MCA.
_get_bus:
call gettrueproc
xor dx, dx ! Assume XT
cmp ax, 286 ! An AT has at least a 286
jb got_bus
inc dx ! Assume AT
movb ah, 0xC0 ! Code for get configuration
int 0x15
jc got_bus ! Carry clear and ah = 00 if supported
testb ah, ah
jne got_bus
eseg movb al, 5(bx) ! Load feature byte #1
inc dx ! Assume MCA
testb al, 0x02 ! Test bit 1 - "bus is Micro Channel"
jnz got_bus
dec dx ! Assume AT
testb al, 0x40 ! Test bit 6 - "2nd 8259 installed"
jnz got_bus
dec dx ! It is an XT
got_bus:
push ds
pop es ! Restore es
mov ax, dx ! Return bus code
mov (bus), ax ! Keep bus code, A20 handler likes to know
ret
! u16_t get_video(void)
! Return type of display, in order: MDA, CGA, mono EGA, color EGA,
! mono VGA, color VGA.
_get_video:
mov ax, 0x1A00 ! Function 1A returns display code
int 0x10 ! al = 1A if supported
cmpb al, 0x1A
jnz no_dc ! No display code function supported
mov ax, 2
cmpb bl, 5 ! Is it a monochrome EGA?
jz got_video
inc ax
cmpb bl, 4 ! Is it a color EGA?
jz got_video
inc ax
cmpb bl, 7 ! Is it a monochrome VGA?
jz got_video
inc ax
cmpb bl, 8 ! Is it a color VGA?
jz got_video
no_dc: movb ah, 0x12 ! Get information about the EGA
movb bl, 0x10
int 0x10
cmpb bl, 0x10 ! Did it come back as 0x10? (No EGA)
jz no_ega
mov ax, 2
cmpb bh, 1 ! Is it monochrome?
jz got_video
inc ax
jmp got_video
no_ega: int 0x11 ! Get bit pattern for equipment
and ax, 0x30 ! Isolate color/mono field
sub ax, 0x30
jz got_video ! Is it an MDA?
mov ax, 1 ! No it's CGA
got_video:
ret
! Function to leave the boot monitor and run Minix.
.define _minix
! void minix(u32_t koff, u32_t kcs, u32_t kds,
! char *bootparams, size_t paramsize, u32_t aout);
_minix:
push bp
mov bp, sp ! Pointer to arguments
mov dx, 0x03F2 ! Floppy motor drive control bits
movb al, 0x0C ! Bits 4-7 for floppy 0-3 are off
outb dx ! Kill the motors
push ds
xor ax, ax ! Vector & BIOS data segments
mov ds, ax
andb (0x043F), 0xF0 ! Clear diskette motor status bits of BIOS
pop ds
cli ! No more interruptions
test (_k_flags), K_I386 ! Minix-386?
jnz minix386
! Call Minix in real mode.
minix86:
push 22(bp) ! Address of a.out headers
push 20(bp)
push 18(bp) ! # bytes of boot parameters
push 16(bp) ! Address of boot parameters
mov dx, cs ! Monitor far return address
mov ax, ret86
cmp (_mem+14), 0 ! Any extended memory? (mem[1].size > 0 ?)
jnz 0f
xor dx, dx ! If no ext mem then monitor not preserved
xor ax, ax
0: push dx ! Push monitor far return address or zero
push ax
mov ax, 8(bp)
mov dx, 10(bp)
call abs2seg
push dx ! Kernel code segment
push 4(bp) ! Kernel code offset
mov ax, 12(bp)
mov dx, 14(bp)
call abs2seg
mov ds, dx ! Kernel data segment
mov es, dx ! Set es to kernel data too
retf ! Make a far call to the kernel
! Call 386 Minix in 386 mode.
minix386:
cseg mov (cs_real-2), cs ! Patch CS and DS into the instructions that
cseg mov (ds_real-2), ds ! reload them when switching back to real mode
mov eax, cr0
orb al, 0x01 ! Set PE (protection enable) bit
o32 mov (msw), eax ! Save as protected mode machine status word
mov dx, ds ! Monitor ds
mov ax, p_gdt ! dx:ax = Global descriptor table
call seg2abs
mov (p_gdt_desc+2), ax
movb (p_gdt_desc+4), dl ! Set base of global descriptor table
mov ax, 12(bp)
mov dx, 14(bp) ! Kernel ds (absolute address)
mov (p_ds_desc+2), ax
0