kernel.asm

来自「参照MINIX3写的操作系统 用GCC+NASM+BOCHS开发」· 汇编 代码 · 共 431 行

ASM
431
字号
P_STACKBASE	equ	0
GSREG		equ	P_STACKBASE
FSREG		equ	GSREG		+ 4
ESREG		equ	FSREG		+ 4
DSREG		equ	ESREG		+ 4
EDIREG		equ	DSREG		+ 4
ESIREG		equ	EDIREG		+ 4
EBPREG		equ	ESIREG		+ 4
KERNELESPREG	equ	EBPREG		+ 4
EBXREG		equ	KERNELESPREG	+ 4
EDXREG		equ	EBXREG		+ 4
ECXREG		equ	EDXREG		+ 4
EAXREG		equ	ECXREG		+ 4
RETADR		equ	EAXREG		+ 4
EIPREG		equ	RETADR		+ 4
CSREG		equ	EIPREG		+ 4
EFLAGSREG	equ	CSREG		+ 4
ESPREG		equ	EFLAGSREG	+ 4
SSREG		equ	ESPREG		+ 4
P_STACKTOP	equ	SSREG		+ 4
P_LDT_SEL	equ	P_STACKTOP
P_LDT		equ	P_LDT_SEL	+ 4


TSS3_S_SP0	equ	4
SELECTOR_FLAT_C		equ		0x08		; LOADER 里面已经确定了的.
SELECTOR_TSS		equ		0x20		; TSS. 从外层跳到内存时 SS 和 ESP 的值从里面获得.
SELECTOR_KERNEL_CS	equ		SELECTOR_FLAT_CINT_M_CTL	equ	0x20	; I/O port for interrupt controller         <Master>
INT_M_CTLMASK	equ	0x21	; setting bits in this port disables ints   <Master>
INT_S_CTL	equ	0xA0	; I/O port for second interrupt controller  <Slave>
INT_S_CTLMASK	equ	0xA1	; setting bits in this port disables ints   <Slave>EOI		equ	0x20
extern IrqTable
extern  ReEnter
extern ReadyProc
extern tss
extern ReadyProc
extern CStart 
extern gdt_ptr
extern idt_ptr
extern ExceptionHandler
extern  SpuriousIrq
extern  Main
extern DisplayString
extern Delay
extern ClockHandler
extern Scheduler
extern	_SystemCall
[SECTION .bss]
StackSpace      resb  2*1024
StackTop:

[SECTION .text]
global _start

global DivideError
global SingleStepException
global Nmi
global BreakPointException
global OverFlow
global BoundsCheck
global InvalidOpcode
global CoprNotAvailable
global DoubleFault
global CoprSegOverrun
global InvalidTss
global SegmentNotPresent
global StackException
global GeneralException
global PageFault
global CoprError
global Hwint00
global Hwint01
global Hwint02
global Hwint03
global Hwint04
global Hwint05
global Hwint06
global Hwint07
global Hwint08
global Hwint09
global Hwint10
global Hwint11
global Hwint12
global Hwint13
global Hwint14
global Hwint15
global Hwint90
global Hwint70
global ChangeProcess
global SystemCall
global ReStart
global Hello1
	_start:
	mov  	esp,StackTop
	sgdt	 [gdt_ptr]
	
	call 	CStart
	lgdt 	[gdt_ptr]
	lidt   	[idt_ptr]
	jmp  SELECTOR_KERNEL_CS:csinit
csinit:
	mov ah,0fh
	mov al,'k'
        mov [gs:((80*22+39)*2)],ax
	
	xor	eax, eax
	mov	ax, SELECTOR_TSS
	ltr	ax

	jmp Main   ;真正跳入C主函数


ReStart:
	mov	esp, [ReadyProc]
	lldt	[esp + P_LDT_SEL] 
	lea	eax, [esp + P_STACKTOP]
	mov	dword [tss + TSS3_S_SP0], eax
ReStart_Reenter:
	dec dword[ReEnter]
	pop 	gs
	pop	fs
	pop	es
	pop	ds
	popad 
	add esp,4
	iretd


DivideError:
	push 0xFFFFFFFF
	push 0
	jmp Exception 
SingleStepException:
        push 0xFFFFFFFF
	push 1
	jmp Exception 
Nmi: 
	push 0xFFFFFFFF
	push 2
	jmp Exception 
BreakPointException:
	push 0xFFFFFFFF
	push 3
	jmp Exception 
OverFlow:
	push 0xFFFFFFFF
	push 4
	jmp Exception 
BoundsCheck:
	push 0xFFFFFFFF
	push 5
	jmp Exception 
InvalidOpcode:
	push 0xFFFFFFFF
	push 6
	jmp Exception 
CoprNotAvailable:
	push 0xFFFFFFFF
	push 7
	jmp Exception 
DoubleFault:
	
	push 8
	jmp Exception 
CoprSegOverrun:
	push 0xFFFFFFFF
	push 9
	jmp Exception 
InvalidTss:
	
	push 10
	jmp Exception 
SegmentNotPresent:
	
	push 11
	jmp Exception 
StackException:
	
	push 12
	jmp Exception 
GeneralException:
        
	push 13
	jmp Exception
PageFault: 

	push 14
	jmp Exception 
CoprError:
	push 0xFFFFFFFF
	push 16
	jmp Exception 
Exception:
	call ExceptionHandler
	add esp,4*2
	hlt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

%macro hwint_master 1
	call Save

	in al,INT_M_CTLMASK
	or al,(1<<%1)
	out INT_M_CTLMASK,al

	mov al,EOI
	out INT_M_CTL,al

	sti
	push %1
	call [IrqTable+4*%1]
	pop ecx
	cli
	
	
	in al,INT_M_CTLMASK
	and al,~(1<<%1)
	out INT_M_CTLMASK,al

        ret
%endmacro
Save:
	pushad
	push ds
	push es
	push fs
	push gs
	mov dx,ss
	mov ds,dx
	mov es,dx	mov fs,dx

	mov esi,esp	
        inc dword [ReEnter]
	cmp dword [ReEnter],0
	jne  .11

  	mov esp,StackTop

	push ReStart
	jmp [esi+RETADR-P_STACKBASE]
.11:
	push ReStart_Reenter
	jmp [esi+RETADR-P_STACKBASE]


ALIGN 16
Hwint00:
        hwint_master 0
ALIGN 16
Hwint01:
	hwint_master 1
ALIGN 16
Hwint02:
	hwint_master 2
ALIGN 16
Hwint03:
	hwint_master 3
ALIGN 16
Hwint04:
	hwint_master 4
ALIGN 16
Hwint05:
	hwint_master 5
ALIGN 16
Hwint06:
	hwint_master 6
ALIGN 16
Hwint07:
	hwint_master 7

%macro hwint_slave 1
	call Save

	in al,INT_S_CTLMASK
	or al,(1<<(%1-8))
	out INT_S_CTLMASK,al

	mov al,EOI
	out INT_S_CTL,al

	sti
	push %1
	call [IrqTable+4*%1]
	pop ecx
	cli
	
	in al,INT_S_CTLMASK
	and al,~(1<<(%1-8))
	out INT_S_CTLMASK,al

        ret
%endmacro

ALIGN 16
Hwint08:
	hwint_slave 8
ALIGN 16
Hwint09:
	hwint_slave 9
ALIGN 16
Hwint10:
	hwint_slave 10
ALIGN 16
Hwint11:
	hwint_slave 11
ALIGN 16
Hwint12:
	hwint_slave 12
ALIGN 16
Hwint13:
	hwint_slave 13
ALIGN 16
Hwint14:
	hwint_slave 14
ALIGN 16
Hwint15:
	hwint_slave 15
Hwint90:
	sub esp,4

	pushad
	push ds
	push es
	push fs
	push gs
	mov dx,ss
	mov ds,dx
	mov es,dx
	
	mov esp,StackTop
	
	call Scheduler
	
	mov	esp, [ReadyProc]

	lldt	[esp + P_LDT_SEL] 
	lea	eax, [esp + P_STACKTOP]
	mov	dword [tss + TSS3_S_SP0], eax
	
	pop 	gs
	pop	fs
	pop	es
	pop	ds
	popad 
	add esp,4
	iretd	
ChangeProcess:
	int 0X90
	ret		
Hwint70:

	sub esp,4
	;call save
	pushad
	push ds
  push es
	push fs
	push gs
	mov dx,ss
	mov ds,dx
	mov es,dx
	
	mov esp,StackTop
  ; sti
	;and eax,0xffff
	;and ebx,0xffff
	;and ecx,0xffff
	push edx
	push ecx
	push ebx
	push eax
	call _SystemCall
	add esp,4*3
    ;cli
    ;ret
    
	mov	esp, [ReadyProc]
	mov [esp+EAXREG-P_STACKBASE],eax
	lldt	[esp + P_LDT_SEL] 
	lea	eax, [esp + P_STACKTOP]
	mov	dword [tss + TSS3_S_SP0], eax
	
	pop 	gs
	pop	fs
	pop	es
	pop	ds
	popad 
	add esp,4
	iretd
	
;	call save
;	sti
;	push ecx
;	push ebx
;	push eax
;	call _SystemCall
;	add esp,4*3
;	mov [esi+EAXREG-P_STACKBASE],eax
;	cli
;	ret
	
SystemCall:
	mov eax,[esp+4]
	mov ebx,[esp+8]
	mov ecx,[esp+12]
	mov edx,[esp+16]
	int 0x70
	ret

Hello1:
	mov eax,100
	int 0x70
	ret




⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?