|
|
1.1 root 1: /*
2: * head.s contains the 32-bit startup code.
3: *
4: * NOTE!!! Startup happens at absolute address 0x00000000, which is also where
5: * the page directory will exist. The startup code will be overwritten by
6: * the page directory.
7: */
1.1.1.2 ! root 8: .code32
1.1 root 9: .text
1.1.1.2 ! root 10: .globl idt,gdt,pg_dir,startup_32
! 11: pg_dir:
1.1 root 12: startup_32:
13: movl $0x10,%eax
14: mov %ax,%ds
15: mov %ax,%es
16: mov %ax,%fs
17: mov %ax,%gs
1.1.1.2 ! root 18: lss stack_start,%esp
1.1 root 19: call setup_idt
20: call setup_gdt
21: movl $0x10,%eax # reload all the segment registers
22: mov %ax,%ds # after changing gdt. CS was already
23: mov %ax,%es # reloaded in 'setup_gdt'
24: mov %ax,%fs
25: mov %ax,%gs
1.1.1.2 ! root 26: lss stack_start,%esp
1.1 root 27: xorl %eax,%eax
28: 1: incl %eax # check that A20 really IS enabled
29: movl %eax,0x000000
30: cmpl %eax,0x100000
31: je 1b
32: movl %cr0,%eax # check math chip
33: andl $0x80000011,%eax # Save PG,ET,PE
34: testl $0x10,%eax
35: jne 1f # ET is set - 387 is present
36: orl $4,%eax # else set emulate bit
37: 1: movl %eax,%cr0
38: jmp after_page_tables
39:
40: /*
41: * setup_idt
42: *
43: * sets up a idt with 256 entries pointing to
44: * ignore_int, interrupt gates. It then loads
45: * idt. Everything that wants to install itself
46: * in the idt-table may do so themselves. Interrupts
47: * are enabled elsewhere, when we can be relatively
48: * sure everything is ok. This routine will be over-
49: * written by the page tables.
50: */
51: setup_idt:
52: lea ignore_int,%edx
53: movl $0x00080000,%eax
54: movw %dx,%ax /* selector = 0x0008 = cs */
55: movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
56:
1.1.1.2 ! root 57: lea idt,%edi
1.1 root 58: mov $256,%ecx
59: rp_sidt:
60: movl %eax,(%edi)
61: movl %edx,4(%edi)
62: addl $8,%edi
63: dec %ecx
64: jne rp_sidt
65: lidt idt_descr
66: ret
67:
68: /*
69: * setup_gdt
70: *
71: * This routines sets up a new gdt and loads it.
72: * Only two entries are currently built, the same
73: * ones that were built in init.s. The routine
74: * is VERY complicated at two whole lines, so this
75: * rather long comment is certainly needed :-).
76: * This routine will beoverwritten by the page tables.
77: */
78: setup_gdt:
79: lgdt gdt_descr
80: ret
81:
82: .org 0x1000
83: pg0:
84:
85: .org 0x2000
86: pg1:
87:
88: .org 0x3000
89: pg2: # This is not used yet, but if you
90: # want to expand past 8 Mb, you'll have
91: # to use it.
92:
93: .org 0x4000
94: after_page_tables:
95: pushl $0 # These are the parameters to main :-)
96: pushl $0
97: pushl $0
98: pushl $L6 # return address for main, if it decides to.
1.1.1.2 ! root 99: pushl $main
1.1 root 100: jmp setup_paging
101: L6:
102: jmp L6 # main should never return here, but
103: # just in case, we know what happens.
104:
105: /* This is the default interrupt "handler" :-) */
106: .align 2
107: ignore_int:
108: incb 0xb8000+160 # put something on the screen
109: movb $2,0xb8000+161 # so that we know something
110: iret # happened
111:
112:
113: /*
114: * Setup_paging
115: *
116: * This routine sets up paging by setting the page bit
117: * in cr0. The page tables are set up, identity-mapping
118: * the first 8MB. The pager assumes that no illegal
119: * addresses are produced (ie >4Mb on a 4Mb machine).
120: *
121: * NOTE! Although all physical memory should be identity
122: * mapped by this routine, only the kernel page functions
123: * use the >1Mb addresses directly. All "normal" functions
124: * use just the lower 1Mb, or the local data space, which
125: * will be mapped to some other place - mm keeps track of
126: * that.
127: *
128: * For those with more memory than 8 Mb - tough luck. I've
129: * not got it, why should you :-) The source is here. Change
130: * it. (Seriously - it shouldn't be too difficult. Mostly
131: * change some constants etc. I left it at 8Mb, as my machine
132: * even cannot be extended past that (ok, but it was cheap :-)
133: * I've tried to show which constants to change by having
134: * some kind of marker at them (search for "8Mb"), but I
135: * won't guarantee that's all :-( )
136: */
137: .align 2
138: setup_paging:
139: movl $1024*3,%ecx
140: xorl %eax,%eax
141: xorl %edi,%edi /* pg_dir is at 0x000 */
142: cld;rep;stosl
1.1.1.2 ! root 143: movl $pg0+7,pg_dir /* set present bit/user r/w */
! 144: movl $pg1+7,pg_dir+4 /* --------- " " --------- */
1.1 root 145: movl $pg1+4092,%edi
146: movl $0x7ff007,%eax /* 8Mb - 4096 + 7 (r/w user,p) */
147: std
148: 1: stosl /* fill pages backwards - more efficient :-) */
149: subl $0x1000,%eax
150: jge 1b
151: xorl %eax,%eax /* pg_dir is at 0x0000 */
152: movl %eax,%cr3 /* cr3 - page directory start */
153: movl %cr0,%eax
154: orl $0x80000000,%eax
155: movl %eax,%cr0 /* set paging (PG) bit */
156: ret /* this also flushes prefetch-queue */
157:
158: .align 2
159: .word 0
160: idt_descr:
161: .word 256*8-1 # idt contains 256 entries
1.1.1.2 ! root 162: .long idt
1.1 root 163: .align 2
164: .word 0
165: gdt_descr:
166: .word 256*8-1 # so does gdt (not that that's any
1.1.1.2 ! root 167: .long gdt # magic number, but it works for me :^)
1.1 root 168:
1.1.1.2 ! root 169: .align 8
! 170: idt: .fill 256,8,0 # idt is uninitialized
1.1 root 171:
1.1.1.2 ! root 172: gdt: .quad 0x0000000000000000 /* NULL descriptor */
1.1 root 173: .quad 0x00c09a00000007ff /* 8Mb */
174: .quad 0x00c09200000007ff /* 8Mb */
175: .quad 0x0000000000000000 /* TEMPORARY - don't use */
176: .fill 252,8,0 /* space for LDT's and TSS's etc */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.