📄 klib386.s
字号:
.align 16_phys_outsb: push ebp mov ebp, esp cld push esi push ds mov ecx, FLAT_DS_SELECTOR mov ds, cx mov edx, 8(ebp) ! port to write to mov esi, 12(ebp) ! source addr mov ecx, 16(ebp) ! byte count rep outsb ! output many bytes pop ds pop esi pop ebp ret!*==========================================================================*!* intr_unmask *!*==========================================================================*/! PUBLIC void intr_unmask(irq_hook_t *hook)! Enable an interrupt request line by clearing an 8259 bit.! Equivalent C code for hook->irq < 8:! if ((irq_actids[hook->irq] &= ~hook->id) == 0)! outb(INT_CTLMASK, inb(INT_CTLMASK) & ~(1 << irq)); .align 16_intr_unmask: push ebp mov ebp, esp pushf cli mov eax, 8(ebp) ! hook mov ecx, 8(eax) ! irq mov eax, 12(eax) ! id bit not eax and _irq_actids(ecx*4), eax ! clear this id bit jnz en_done ! still masked by other handlers? movb ah, ~1 rolb ah, cl ! ah = ~(1 << (irq % 8)) mov edx, INT_CTLMASK ! enable irq < 8 at the master 8259 cmpb cl, 8 jb 0f mov edx, INT2_CTLMASK ! enable irq >= 8 at the slave 82590: inb dx andb al, ah outb dx ! clear bit at the 8259en_done:popf leave ret!*==========================================================================*!* intr_mask *!*==========================================================================*/! PUBLIC int intr_mask(irq_hook_t *hook)! Disable an interrupt request line by setting an 8259 bit.! Equivalent C code for irq < 8:! irq_actids[hook->irq] |= hook->id;! outb(INT_CTLMASK, inb(INT_CTLMASK) | (1 << irq));! Returns true iff the interrupt was not already disabled. .align 16_intr_mask: push ebp mov ebp, esp pushf cli mov eax, 8(ebp) ! hook mov ecx, 8(eax) ! irq mov eax, 12(eax) ! id bit or _irq_actids(ecx*4), eax ! set this id bit movb ah, 1 rolb ah, cl ! ah = (1 << (irq % 8)) mov edx, INT_CTLMASK ! disable irq < 8 at the master 8259 cmpb cl, 8 jb 0f mov edx, INT2_CTLMASK ! disable irq >= 8 at the slave 82590: inb dx testb al, ah jnz dis_already ! already disabled? orb al, ah outb dx ! set bit at the 8259 mov eax, 1 ! disabled by this function popf leave retdis_already: xor eax, eax ! already disabled popf leave ret!*===========================================================================*!* phys_copy *!*===========================================================================*! PUBLIC void phys_copy(phys_bytes source, phys_bytes destination,! phys_bytes bytecount);! Copy a block of physical memory.PC_ARGS = 4 + 4 + 4 + 4 ! 4 + 4 + 4! es edi esi eip src dst len .align 16_phys_copy: cld push esi push edi push es mov eax, FLAT_DS_SELECTOR mov es, ax mov esi, PC_ARGS(esp) mov edi, PC_ARGS+4(esp) mov eax, PC_ARGS+4+4(esp) cmp eax, 10 ! avoid align overhead for small counts jb pc_small mov ecx, esi ! align source, hope target is too neg ecx and ecx, 3 ! count for alignment sub eax, ecx rep eseg movsb mov ecx, eax shr ecx, 2 ! count of dwords rep eseg movs and eax, 3pc_small: xchg ecx, eax ! remainder rep eseg movsb pop es pop edi pop esi ret!*===========================================================================*!* phys_memset *!*===========================================================================*! PUBLIC void phys_memset(phys_bytes source, unsigned long pattern,! phys_bytes bytecount);! Fill a block of physical memory with pattern. .align 16_phys_memset: push ebp mov ebp, esp push esi push ebx push ds mov esi, 8(ebp) mov eax, 16(ebp) mov ebx, FLAT_DS_SELECTOR mov ds, bx mov ebx, 12(ebp) shr eax, 2fill_start: mov (esi), ebx add esi, 4 dec eax jnz fill_start ! Any remaining bytes? mov eax, 16(ebp) and eax, 3remain_fill: cmp eax, 0 jz fill_done movb bl, 12(ebp) movb (esi), bl add esi, 1 inc ebp dec eax jmp remain_fillfill_done: pop ds pop ebx pop esi pop ebp ret!*===========================================================================*!* mem_rdw *!*===========================================================================*! PUBLIC u16_t mem_rdw(U16_t segment, u16_t *offset);! Load and return word at far pointer segment:offset. .align 16_mem_rdw: mov cx, ds mov ds, 4(esp) ! segment mov eax, 4+4(esp) ! offset movzx eax, (eax) ! word to return mov ds, cx ret!*===========================================================================*!* reset *!*===========================================================================*! PUBLIC void reset();! Reset the system by loading IDT with offset 0 and interrupting._reset: lidt (idt_zero) int 3 ! anything goes, the 386 will not like it.sect .dataidt_zero: .data4 0, 0.sect .text!*===========================================================================*!* idle_task *!*===========================================================================*_idle_task:! This task is called when the system has nothing else to do. The HLT! instruction puts the processor in a state where it draws minimum power. push halt call _level0 ! level0(halt) pop eax jmp _idle_taskhalt: sti hlt cli ret!*===========================================================================*!* level0 *!*===========================================================================*! PUBLIC void level0(void (*func)(void))! Call a function at permission level 0. This allows kernel tasks to do! things that are only possible at the most privileged CPU level.!_level0: mov eax, 4(esp) mov (_level0_func), eax int LEVEL0_VECTOR ret!*===========================================================================*!* read_flags *!*===========================================================================*! PUBLIC unsigned long read_cpu_flags(void);! Read CPU status flags from C..align 16_read_cpu_flags: pushf mov eax, (esp) popf ret!*===========================================================================*!* read_cr0 *!*===========================================================================*! PUBLIC unsigned long read_cr0(void);_read_cr0: push ebp mov ebp, esp mov eax, cr0 pop ebp ret!*===========================================================================*!* write_cr0 *!*===========================================================================*! PUBLIC void write_cr0(unsigned long value);_write_cr0: push ebp mov ebp, esp mov eax, 8(ebp) mov cr0, eax jmp 0f ! A jump is required for some flags0: pop ebp ret!*===========================================================================*!* write_cr3 *!*===========================================================================*! PUBLIC void write_cr3(unsigned long value);_write_cr3: push ebp mov ebp, esp mov eax, 8(ebp) mov cr3, eax pop ebp ret
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -