| 1 |
|
| 2 |
#include <mach/machine/asm.h> |
| 3 |
#include <mach/machine/multiboot.h> |
| 4 |
#include <i386/apic.h> |
| 5 |
#include <i386/seg.h> |
| 6 |
#include <i386/i386asm.h> |
| 7 |
|
| 8 |
#define BOOT_CS 0x8 |
| 9 |
#define BOOT_DS 0x10 |
| 10 |
|
| 11 |
#ifdef __ELF__ |
| 12 |
#define AOUT_KLUDGE 0 |
| 13 |
#else |
| 14 |
#define AOUT_KLUDGE MULTIBOOT_AOUT_KLUDGE |
| 15 |
#endif /* __ELF__ */ |
| 16 |
#define MULTIBOOT_FLAGS MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_VIDEO_MODE | AOUT_KLUDGE |
| 17 |
|
| 18 |
/* |
| 19 |
* This section will be put first into .text. See also i386/ldscript. |
| 20 |
*/ |
| 21 |
.section .text.start,"ax" |
| 22 |
|
| 23 |
/* We should never be entered this way. */ |
| 24 |
.globl start,_start |
| 25 |
start: |
| 26 |
_start: |
| 27 |
jmp boot_entry |
| 28 |
|
| 29 |
/* MultiBoot header - see multiboot.h. */ |
| 30 |
P2ALIGN(2) |
| 31 |
boot_hdr: |
| 32 |
.long MULTIBOOT_MAGIC |
| 33 |
.long MULTIBOOT_FLAGS |
| 34 |
/* |
| 35 |
* The next item here is the checksum. |
| 36 |
* XX this works OK until we need at least the 30th bit. |
| 37 |
*/ |
| 38 |
.long - (MULTIBOOT_MAGIC+MULTIBOOT_FLAGS) |
| 39 |
#ifndef __ELF__ /* a.out kludge */ |
| 40 |
.long boot_hdr /* header_addr */ |
| 41 |
.long _start /* load_addr */ |
| 42 |
.long _edata /* load_end_addr */ |
| 43 |
.long _end /* bss_end_addr */ |
| 44 |
.long boot_entry /* entry */ |
| 45 |
#else |
| 46 |
.long 0 |
| 47 |
.long 0 |
| 48 |
.long 0 |
| 49 |
.long 0 |
| 50 |
.long 0 |
| 51 |
#endif /* __ELF__ */ |
| 52 |
.long MULTIBOOT_VIDEO_MODE_TYPE_EGA_TEXT |
| 53 |
.long 80 |
| 54 |
.long 25 |
| 55 |
.long MULTIBOOT_VIDEO_PARAM_NO_PREFERENCE |
| 56 |
|
| 57 |
boot_entry: |
| 58 |
movl $percpu_array - KERNELBASE, %eax |
| 59 |
movw %ax, boot_percpu_low - KERNELBASE |
| 60 |
shr $16, %eax |
| 61 |
movb %al, boot_percpu_med - KERNELBASE |
| 62 |
shr $8, %ax |
| 63 |
movb %al, boot_percpu_high - KERNELBASE |
| 64 |
|
| 65 |
/* use segmentation to offset ourself. */ |
| 66 |
lgdt boot_gdt_descr - KERNELBASE |
| 67 |
ljmp $BOOT_CS,$0f |
| 68 |
0: |
| 69 |
xorw %ax,%ax |
| 70 |
movw %ax,%ds |
| 71 |
movw %ax,%es |
| 72 |
movw %ax,%fs |
| 73 |
movw %ax,%gs |
| 74 |
movw $BOOT_DS,%ax |
| 75 |
movw %ax,%ds |
| 76 |
movw %ax,%es |
| 77 |
movw %ax,%ss |
| 78 |
movw $0x68,%ax |
| 79 |
movw %ax,%gs |
| 80 |
|
| 81 |
/* Switch to our own interrupt stack. */ |
| 82 |
movl $solid_intstack+INTSTACK_SIZE-4, %esp |
| 83 |
andl $0xfffffff0,%esp |
| 84 |
|
| 85 |
#ifdef APIC |
| 86 |
/* Enable local apic in xAPIC mode */ |
| 87 |
xorl %eax, %eax |
| 88 |
xorl %edx, %edx |
| 89 |
movl $APIC_MSR, %ecx |
| 90 |
rdmsr |
| 91 |
orl $APIC_MSR_ENABLE, %eax |
| 92 |
orl $APIC_MSR_BSP, %eax |
| 93 |
andl $(~APIC_MSR_X2APIC), %eax |
| 94 |
movl $APIC_MSR, %ecx |
| 95 |
wrmsr |
| 96 |
#endif |
| 97 |
|
| 98 |
/* Reset EFLAGS to a known state. */ |
| 99 |
pushl $0 |
| 100 |
popf |
| 101 |
|
| 102 |
/* Clear uninitialized data. */ |
| 103 |
lea _edata,%edi |
| 104 |
lea _end,%ecx |
| 105 |
subl %edi,%ecx |
| 106 |
xorl %eax,%eax |
| 107 |
rep |
| 108 |
stosb |
| 109 |
|
| 110 |
/* Push the boot_info pointer to be the second argument. */ |
| 111 |
pushl %ebx |
| 112 |
|
| 113 |
/* Fix ifunc entries */ |
| 114 |
movl $__rel_iplt_start,%esi |
| 115 |
movl $__rel_iplt_end,%edi |
| 116 |
iplt_cont: |
| 117 |
cmpl %edi,%esi |
| 118 |
jae iplt_done |
| 119 |
movl (%esi),%ebx /* r_offset */ |
| 120 |
movb 4(%esi),%al /* info */ |
| 121 |
cmpb $42,%al /* IRELATIVE */ |
| 122 |
jnz iplt_next |
| 123 |
call *(%ebx) /* call ifunc */ |
| 124 |
movl %eax,(%ebx) /* fixed address */ |
| 125 |
iplt_next: |
| 126 |
addl $8,%esi |
| 127 |
jmp iplt_cont |
| 128 |
iplt_done: |
| 129 |
|
| 130 |
/* Jump into C code. */ |
| 131 |
call EXT(c_boot_entry) |
| 132 |
|
| 133 |
.align 16 |
| 134 |
.word 0 |
| 135 |
boot_gdt_descr: |
| 136 |
.word 14*8-1 |
| 137 |
.long boot_gdt - KERNELBASE |
| 138 |
.align 16 |
| 139 |
boot_gdt: |
| 140 |
/* 0 */ |
| 141 |
.quad 0 |
| 142 |
|
| 143 |
/* boot CS = 0x08 */ |
| 144 |
.word 0xffff |
| 145 |
.word (-KERNELBASE) & 0xffff |
| 146 |
.byte ((-KERNELBASE) >> 16) & 0xff |
| 147 |
.byte ACC_PL_K | ACC_CODE_R | ACC_P |
| 148 |
.byte ((SZ_32 | SZ_G) << 4) | 0xf |
| 149 |
.byte ((-KERNELBASE) >> 24) & 0xff |
| 150 |
|
| 151 |
/* boot DS = 0x10 */ |
| 152 |
.word 0xffff |
| 153 |
.word (-KERNELBASE) & 0xffff |
| 154 |
.byte ((-KERNELBASE) >> 16) & 0xff |
| 155 |
.byte ACC_PL_K | ACC_DATA_W | ACC_P |
| 156 |
.byte ((SZ_32 | SZ_G) << 4) | 0xf |
| 157 |
.byte ((-KERNELBASE) >> 24) & 0xff |
| 158 |
|
| 159 |
/* LDT = 0x18 */ |
| 160 |
.quad 0 |
| 161 |
|
| 162 |
/* TSS = 0x20 */ |
| 163 |
.quad 0 |
| 164 |
|
| 165 |
/* USER_LDT = 0x28 */ |
| 166 |
.quad 0 |
| 167 |
|
| 168 |
/* USER_TSS = 0x30 */ |
| 169 |
.quad 0 |
| 170 |
|
| 171 |
/* LINEAR = 0x38 */ |
| 172 |
.quad 0 |
| 173 |
|
| 174 |
/* FPREGS = 0x40 */ |
| 175 |
.quad 0 |
| 176 |
|
| 177 |
/* USER_GDT = 0x48 and 0x50 */ |
| 178 |
.quad 0 |
| 179 |
.quad 0 |
| 180 |
|
| 181 |
/* USER_TSS64 = 0x58 */ |
| 182 |
.quad 0 |
| 183 |
|
| 184 |
/* USER_TSS64 = 0x60 */ |
| 185 |
.quad 0 |
| 186 |
|
| 187 |
/* boot GS = 0x68 */ |
| 188 |
.word 0xffff |
| 189 |
boot_percpu_low: |
| 190 |
.word 0 |
| 191 |
boot_percpu_med: |
| 192 |
.byte 0 |
| 193 |
.byte ACC_PL_K | ACC_DATA_W | ACC_P |
| 194 |
.byte ((SZ_32 | SZ_G) << 4) | 0xf |
| 195 |
boot_percpu_high: |
| 196 |
.byte 0 |