0.00-050613_head.s

  1 #  head.s contains the 32-bit startup code.
  2 #  Two L3 task multitasking. The code of tasks are in kernel area, 
  3 #  just like the Linux. The kernel code is located at 0x10000. 
  4 SCRN_SEL    = 0x18
  5 TSS0_SEL    = 0x20
  6 LDT0_SEL    = 0x28
  7 TSS1_SEL    = 0X30
  8 LDT1_SEL    = 0x38
  9 
 10 .text
 11 startup_32:
 12     movl $0x10,%eax
 13     mov %ax,%ds
 14 #    mov %ax,%es
 15     lss init_stack,%esp
 16 
 17 # setup base fields of descriptors.
 18     call setup_idt
 19     call setup_gdt
 20     movl $0x10,%eax        # reload all the segment registers
 21     mov %ax,%ds        # after changing gdt. 
 22     mov %ax,%es
 23     mov %ax,%fs
 24     mov %ax,%gs
 25     lss init_stack,%esp
 26 
 27 # setup up timer 8253 chip.
 28     movb $0x36, %al
 29     movl $0x43, %edx
 30     outb %al, %dx
 31     movl $11930, %eax        # timer frequency 100 HZ 
 32     movl $0x40, %edx
 33     outb %al, %dx
 34     movb %ah, %al
 35     outb %al, %dx
 36 
 37 # setup timer & system call interrupt descriptors.
 38     movl $0x00080000, %eax    
 39     movw $timer_interrupt, %ax
 40     movw $0x8E00, %dx
 41     movl $0x08, %ecx              # The PC default timer int.
 42     lea idt(,%ecx,8), %esi
 43     movl %eax,(%esi) 
 44     movl %edx,4(%esi)
 45     movw $system_interrupt, %ax
 46     movw $0xef00, %dx
 47     movl $0x80, %ecx
 48     lea idt(,%ecx,8), %esi
 49     movl %eax,(%esi) 
 50     movl %edx,4(%esi)
 51 
 52 # unmask the timer interrupt.
 53 #    movl $0x21, %edx
 54 #    inb %dx, %al
 55 #    andb $0xfe, %al
 56 #    outb %al, %dx
 57 
 58 # Move to user mode (task 0)
 59     pushfl
 60     andl $0xffffbfff, (%esp)
 61     popfl
 62     movl $TSS0_SEL, %eax
 63     ltr %ax
 64     movl $LDT0_SEL, %eax
 65     lldt %ax 
 66     movl $0, current
 67     sti
 68     pushl $0x17
 69     pushl $init_stack
 70     pushfl
 71     pushl $0x0f
 72     pushl $task0
 73     iret
 74 
 75 /****************************************/
 76 setup_gdt:
 77     lgdt lgdt_opcode
 78     ret
 79 
 80 setup_idt:
 81     lea ignore_int,%edx
 82     movl $0x00080000,%eax
 83     movw %dx,%ax        /* selector = 0x0008 = cs */
 84     movw $0x8E00,%dx    /* interrupt gate - dpl=0, present */
 85     lea idt,%edi
 86     mov $256,%ecx
 87 rp_sidt:
 88     movl %eax,(%edi)
 89     movl %edx,4(%edi)
 90     addl $8,%edi
 91     dec %ecx
 92     jne rp_sidt
 93     lidt lidt_opcode
 94     ret
 95 
 96 # -----------------------------------
 97 write_char:
 98     push %gs
 99     pushl %ebx
100 #    pushl %eax
101     mov $SCRN_SEL, %ebx
102     mov %bx, %gs
103     movl scr_loc, %bx
104     shl $1, %ebx
105     movb %al, %gs:(%ebx)
106     shr $1, %ebx
107     incl %ebx
108     cmpl $2000, %ebx
109     jb 1f
110     movl $0, %ebx
111 1:    movl %ebx, scr_loc    
112 #    popl %eax
113     popl %ebx
114     pop %gs
115     ret
116 
117 /***********************************************/
118 /* This is the default interrupt "handler" :-) */
119 .align 2
120 ignore_int:
121     push %ds
122     pushl %eax
123     movl $0x10, %eax
124     mov %ax, %ds
125     movl $67, %eax            /* print 'C' */
126     call write_char
127     popl %eax
128     pop %ds
129     iret
130 
131 /* Timer interrupt handler */ 
132 .align 2
133 timer_interrupt:
134     push %ds
135     pushl %eax
136     movl $0x10, %eax
137     mov %ax, %ds
138     movb $0x20, %al
139     outb %al, $0x20
140     movl $1, %eax
141     cmpl %eax, current
142     je 1f
143     movl %eax, current
144     ljmp $TSS1_SEL, $0
145     jmp 2f
146 1:    movl $0, current
147     ljmp $TSS0_SEL, $0
148 2:    popl %eax
149     pop %ds
150     iret
151 
152 /* system call handler */
153 .align 2
154 system_interrupt:
155     push %ds
156     pushl %edx
157     pushl %ecx
158     pushl %ebx
159     pushl %eax
160     movl $0x10, %edx
161     mov %dx, %ds
162     call write_char
163     popl %eax
164     popl %ebx
165     popl %ecx
166     popl %edx
167     pop %ds
168     iret
169 
170 /*********************************************/
171 current:.long 0
172 scr_loc:.long 0
173 
174 .align 2
175 lidt_opcode:
176     .word 256*8-1        # idt contains 256 entries
177     .long idt        # This will be rewrite by code. 
178 lgdt_opcode:
179     .word (end_gdt-gdt)-1    # so does gdt 
180     .long gdt        # This will be rewrite by code.
181 
182     .align 3
183 idt:    .fill 256,8,0        # idt is uninitialized
184 
185 gdt:    .quad 0x0000000000000000    /* NULL descriptor */
186     .quad 0x00c09a00000007ff    /* 8Mb 0x08, base = 0x00000 */
187     .quad 0x00c09200000007ff    /* 8Mb 0x10 */
188     .quad 0x00c0920b80000002    /* screen 0x18 - for display */
189 
190     .word 0x0068, tss0, 0xe900, 0x0    # TSS0 descr 0x20
191     .word 0x0040, ldt0, 0xe200, 0x0    # LDT0 descr 0x28
192     .word 0x0068, tss1, 0xe900, 0x0    # TSS1 descr 0x30
193     .word 0x0040, ldt1, 0xe200, 0x0    # LDT1 descr 0x38
194 end_gdt:
195     .fill 128,4,0
196 init_stack:                          # Will be used as user stack for task0.
197     .long init_stack
198     .word 0x10
199 
200 /*************************************/
201 .align 3
202 ldt0:    .quad 0x0000000000000000
203     .quad 0x00c0fa00000003ff    # 0x0f, base = 0x00000
204     .quad 0x00c0f200000003ff    # 0x17
205 
206 tss0:    .long 0             /* back link */
207     .long krn_stk0, 0x10        /* esp0, ss0 */
208     .long 0, 0, 0, 0, 0        /* esp1, ss1, esp2, ss2, cr3 */
209     .long 0, 0, 0, 0, 0        /* eip, eflags, eax, ecx, edx */
210     .long 0, 0, 0, 0, 0        /* ebx esp, ebp, esi, edi */
211     .long 0, 0, 0, 0, 0, 0         /* es, cs, ss, ds, fs, gs */
212     .long LDT0_SEL, 0x8000000    /* ldt, trace bitmap */
213 
214     .fill 128,4,0
215 krn_stk0:
216 #    .long 0
217 
218 /************************************/
219 .align 3
220 ldt1:    .quad 0x0000000000000000
221     .quad 0x00c0fa00000003ff    # 0x0f, base = 0x00000
222     .quad 0x00c0f200000003ff    # 0x17
223 
224 tss1:    .long 0             /* back link */
225     .long krn_stk1, 0x10        /* esp0, ss0 */
226     .long 0, 0, 0, 0, 0        /* esp1, ss1, esp2, ss2, cr3 */
227     .long task1, 0x200        /* eip, eflags */
228     .long 0, 0, 0, 0        /* eax, ecx, edx, ebx */
229     .long usr_stk1, 0, 0, 0        /* esp, ebp, esi, edi */
230     .long 0x17,0x0f,0x17,0x17,0x17,0x17 /* es, cs, ss, ds, fs, gs */
231     .long LDT1_SEL, 0x8000000    /* ldt, trace bitmap */
232 
233     .fill 128,4,0
234 krn_stk1:
235 
236 /************************************/
237 task0:
238     movl $0x17, %eax
239     movw %ax, %ds
240     movl $65, %al              /* print 'A' */
241     int $0x80
242     movl $0xfff, %ecx
243 1:    loop 1b
244     jmp task0 
245 
246 task1:
247     movl $0x17, %eax
248     movw %ax, %ds
249     movl $66, %al              /* print 'B' */
250     int $0x80
251     movl $0xfff, %ecx
252 1:    loop 1b
253     jmp task1
254 
255     .fill 128,4,0 
256 usr_stk1:

 

C

 

posted @ 2015-12-29 16:07  CodeSkill  阅读(196)  评论(0编辑  收藏  举报