文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C-os已经跑起来了

C-os已经跑起来了

时间:2007-05-01  来源:tthacker

昨天终于把C-os跑起来了,它目前只有2个任务,在时钟中断的控制下进行任务切换,task-A显示
"__bill gates like windows__",task-B显示"__linus like linux__",哈哈





以下是源代码:

/*
    boot.s V 0.01 (c) wzt

    It only loads kernel into the memory in this version.
*/

.code16
.align 2
.text
.globl _start

BOOTSEG    = 0x07c0
SYSSEG     = 0x1000
SYSLEN     = 17
BOCHS     = 1

_start:

    ljmp $BOOTSEG, $go
go:
    movw %cs,%ax
    movw %ax,%ds
    movw %ax,%es
    movw %ax,%ss
    movw $0x0400, %sp

    /*    
        clear screen using bios interrupt,beacuse bochs display its dump
        messages.
    */
.ifdef BOCHS
    movw $3,%ax
    int $0x10
.endif

    /* display a boot message */
    movw $boot_msg,%ax
    movw %ax,%bp
    movw $17,%cx
    movw $0x1301,%ax
    movw $0x00c,%bx
    movb $0,%dl
    int $0x10

    /* load kernel into memroy from disk,it use bios interrupt routine 0x13 */
load_system:
    xorw %dx,%dx
    movw $0x02,%cx
    movw $SYSSEG,%ax
    movw %ax,%es
    xorw %bx,%bx
    movb $0x02,%ah
    movb $SYSLEN,%al
    int $0x13
    jnc ok_load
die:
    movw $load_failed,%ax
    movw %ax,%bp
    movw $20,%cx
    movw $0x1301,%ax
    movw $0x00c,%bx
    movb $0,%dl
    int $0x10
    jmp die

    /* first,close interrupt,and then move the kernel into memory 0x1000.*/
ok_load:
    cli    
    movw $SYSSEG,%ax
    movw %ax,%ds
    xorw %si,%si
    xorw %ax,%ax
    movw %ax,%es
    xorw %di,%di
    movw $0x1000,%cx
    cld
    rep movsw

    /* setup IDT & GDT */
    movw $BOOTSEG,%ax
    movw %ax,%ds
    lidt idt_48
    lgdt gdt_48

    /* move to protect mode */
    movw $0x0001,%ax
    lmsw %ax
    ljmp $8,$0

gdt:
desc_null:
    .quad 0x0             #0x0
desc_code:         
    .word 0x07ff            #0x08
    .word 0x0000
    .word 0x9a00
    .word 0x00c0
desc_data:
    .quad 0x00c09200000007ff #0x10

gdt_48:
    .word 0x7ff
    .word 0x7c00 + gdt,0
    
idt_48:
    .word 0
    .word 0,0

boot_msg:
    .asciz "Loading kernel..."
load_fialed:    
    .asciz "Load failed..."

.org 510
    .word 0xaa55




/*
 *    C-os V 0.0.6    (C) wzt    2007
 *
 *    This is my simple kernel,it takes my much time to run it successfully in ring3,
 *    and i like it very much,haha.It has two tasks change with the contrl of time interrupt.
 * When the kernel runs task-A,the task display a message"__bill gates like windows__",
 * while the task-B display a message"__linus like linux__".
 *
*/

.text
.global startup_32
startup_32:
    movl $0x10,%eax
    mov %ax,%ds
#    mov %ax,%es
    lss init_stack,%esp

    call init8259A

    /* setup idt table and gdt table */
    call setup_idt
    call setup_gdt
    movl $0x10,%eax
    mov %ax,%ds
    mov %ax,%es
    mov %ax,%fs
    mov %ax,%gs
    lss init_stack,%esp

    /* set 8253 chip */

    movb $0x36, %al
    movl $0x43, %edx
    outb %al, %dx
    movl $119300, %eax # timer frequency 100 HZ
    movl $0x40, %edx
    outb %al, %dx
    movb %ah, %al
    outb %al, %dx

    /* set timer interrupt */
       movl $0x00080000, %eax
    movw $timer_interrupt, %ax
       movw $0x8E00, %dx
    movl $0x20, %ecx # The PC default timer int.
    lea idt(,%ecx,8), %esi
       movl %eax,(%esi)
    movl %edx,4(%esi)

    /* set system call interrupt */
       movw $system_interrupt, %ax
       movw $0xef00, %dx
    movl $0x85, %ecx
    lea idt(,%ecx,8), %esi
    movl %eax,(%esi)
       movl %edx,4(%esi)

pm_mode_test:
    movl $(80 * 1 + 0 ) * 2,%edi
    movl $ring0_msg,%esi
    call write_string

    movl $(80 * 6 + 0 ) * 2,scr_loc

    /* move to ring3.*/
    pushfl
    andl $0xffffbfff, (%esp)
    popfl
    movl $0x20,%eax
    ltr %ax
    movl $0x28,%eax
    lldt %ax
    movl $0,current
    sti
    pushl $0x17
    pushl $init_stack
    pushfl
    pushl $0x0f
    pushl $ring3_task0
    iret

    /* ignore interrupt,only display a message.*/    
.align 2
ignore_int:
    push %ds
    pushl %eax
    pushl %edi
    movl $0x10,%eax
    mov %ax,%ds
    movl $ignore_msg,%esi
  # movl $(80 * 3 + 0 ) * 2,%edi
    movl scr_loc,%edi
    call write_string
    popl %edi
    popl %eax
    pop %ds
    iret

    /* like schedule,change tasks.*/
.align 2
timer_interrupt:
    push %ds
    pushl %eax
    movl $0x10,%eax
    mov %ax,%ds
    movb $0x20,%al
    outb %al, $0x20
    movl $1,%eax
    cmpl %eax,current
    je 13f
    movl %eax,current
    ljmp $0x30,$0
    jmp 14f
13:
    movl $0,current
    ljmp $0x20,$0
14:
    popl %eax
    pop %ds
    iret

    /* system interrupt,only display a message.*/
.align 2
system_interrupt:
    push %ds
    pushl %edx
    pushl %ecx
    pushl %ebx
    pushl %eax
    movl $0x10, %edx
    mov %dx, %ds
    movw $0x18,%bx
    movw %bx,%gs
    movb $0x0c,%ah
    cld
2:
    movl scr_loc,%ebx
    shl $1,%ebx
    lodsb
    testb %al,%al
    jz 5f
    movw %ax,%gs:(%ebx)
1:
    shr $1,%ebx
    incl %ebx
    cmpl $2000,%ebx
    jb 4f
    movl $0,%ebx
    movl $(80 * 6 + 0 ) * 2,scr_loc
    incb %ah
    jmp 2b
4:
    movl %ebx,scr_loc
    jmp 2b
5:
    popl %eax
    popl %ebx
    popl %ecx
    popl %edx
    pop %ds
    iret

io_delay:
    nop
    nop
    nop
    nop
    ret

init8259A:
    movb $0x11,%al
    outb %al,$0x20
    call io_delay

    outb %al,$0xa0
    call io_delay

    movb $0x20,%al
    outb %al,$0x21
    call io_delay

    movb $0x28,%al
    outb %al,$0xa1
    call io_delay

    movb $0x04,%al
    outb %al,$0x21
    call io_delay

    movb $0x02,%al
    outb %al,$0xa1
    call io_delay

    movb $0x01,%al
    outb %al,$0x21
    call io_delay

    outb %al,$0xa1
    call io_delay

    movb $0xfe,%al
    outb %al,$0x21
    call io_delay

    movb $0xff,%al
    outb %al,$0xa1
    call io_delay

    ret

write_string:
    push %gs
    push %bx
    push %ax
    movw $0x18,%bx
    movw %bx,%gs
    movb $0x0c,%ah
    cld
7:
    lodsb
    testb %al,%al
    jz 8f
    movw %ax,%gs:(%edi)
    addl $2,%edi
    jmp 7b
8:
    pop %ax
    pop %bx
    pop %gs
    ret
 
current:
    .long 0
scr_loc:
    .long 0

setup_gdt:
    lgdt gdt_48
    ret

setup_idt:
    lea ignore_int,%edx
    movl $0x00080000,%eax
    movw %dx,%ax
    movw $0x8e00,%dx
    lea idt,%edi
    movl $256,%ecx
loop_idt:
    movl %eax,(%edi)
    movl %edx,4(%edi)
    addl $8,%edi
    dec %ecx
    jne loop_idt
    lidt idt_48
    ret

.align 2
idt_48:
    .word 256*8-1
    .long idt
gdt_48:
    .word (end_gdt - gdt) - 1
    .long gdt

.align 8
idt:    
    .fill 256,8,0

gdt:
desc_null:
       .quad 0x0000000000000000 #0x0
desc_code:
    .word 0x07ff #0x08
    .word 0x0000
    .word 0x9a00
    .word 0x00c0
desc_data:
    .quad 0x00c09200000007ff #0x10
desc_video:
    .quad 0x00c0f20b80000002 #0x18,DPL=3
desc_tss0: #0x20
    .word 0x0068
    .word tss0
    .word 0xe900
    .word 0x0000
desc_ldt0:                            #0x28
    .word 0x0040
    .word ldt0                        #base addr
    .word 0xe200                    #DPL=3,data
    .word 0x0000
desc_tss1: #0x30
    .word 0x0068
    .word tss1
    .word 0xe900
    .word 0x0000
desc_ldt1:                            #0x38
    .word 0x0040
    .word ldt1 #base addr
    .word 0xe200 #DPL=3,data
    .word 0x0000

end_gdt:                
        
    .fill 128,4,0
init_stack:                            #aslo can be used with ring0 stack
    .long init_stack
    .word 0x10

    .align 8
ldt0:
    .quad 0x000000000000000            #0x4
code0:
    .word 0x03ff                    #0x0f
    .word 0x0000
    .word 0xfa00
    .word 0x00c0
data0:
    .word 0x03ff                    #0x17
    .word 0x0000
    .word 0xf200
    .word 0x00c0

tss0:
    .long 0
    .long ring3_stack, 0x10
    .long 0, 0, 0, 0, 0
    .long 0, 0, 0, 0, 0
    .long 0, 0, 0, 0, 0
    .long 0, 0, 0, 0, 0, 0
    .long 0x28,0x8000000

    .fill 128,4,0
ring3_stack:

    .align 8
ldt1:
    .quad 0x000000000000000 #0x4
code1:
    .word 0x03ff #0x0f
    .word 0x0000
    .word 0xfa00
    .word 0x00c0
data1:
    .word 0x03ff #0x17
    .word 0x0000
    .word 0xf200
    .word 0x00c0

tss1:
    .long 0
    .long ring3_stack1, 0x10
    .long 0, 0, 0, 0, 0
    .long ring3_task1,0x200
    .long 0, 0, 0, 0
    .long usr_stk1,0,0,0
    .long 0x17,0x0f,0x17,0x17,0x17,0x17
    .long 0x38,0x8000000

    .fill 128,4,0
ring3_stack1:

pm_msg:
    .asciz "move to protect mode."
ring3_msg:
    .asciz "moved to Ring3."
ring3_msga:
    .asciz "moved to ring3,starting task-A."
ring3_msgb:
    .asciz "moved to ring3,starting task-B."
ring0_msg:
    .asciz "moved to Ring0."
ignore_msg:
    .asciz "ignore interrupt."
timer_msg:
    .asciz "time interrupt."
task_a:
    .asciz "__bill gates like windows__"
task_b:
    .asciz "__linus like linux__"

ring3_task0:
    movl $0x17,%eax
    movw %ax,%ds
    movl $(80 * 2 + 0 ) * 2,%edi
    movl $ring3_msga,%esi
    call write_string
    movl $task_a,%esi
    int $0x85
    movl $0xff,%ecx
11:
    loop 11b
    jmp ring3_task0

ring3_task1:
    movl $(80 * 2 + 0 ) * 2,%edi
    movl $ring3_msgb,%esi
    call write_string
    movl $task_b,%esi
    int $0x85
    movl $0xff,%ecx
12:
    loop 12b
    jmp ring3_task1

    .fill 128,4,0
usr_stk1:

排行榜 更多 +
僵尸猎手小明

僵尸猎手小明

动作格斗 下载
狩猎波比

狩猎波比

动作格斗 下载
花海相机

花海相机

图像拍照 下载