debuggers.hg

annotate xen/arch/x86/boot/x86_64.S @ 3632:fec8b1778268

bitkeeper revision 1.1159.212.60 (41febc4bKKSkh9u-Zes9v2CmBuLZxA)

More bootstrap fixes for x86/64. Next thing to do is sort out the IDT and
get traps.c working; then we can get rid of a bunch of dummy labels from
end of boot/x86_64.S. We're also going to need some kind of entry.S before
we can safely enable interrupts. Also bear in mind that not all of physical
RAM may be mapped (only first 1GB) and no m2p table is yet allocated or
mapped. Plenty to be done!
author kaf24@viper.(none)
date Mon Jan 31 23:16:27 2005 +0000 (2005-01-31)
parents eef1949801b8
children ed902e5c4b49
rev   line source
kaf24@1694 1 #include <xen/config.h>
kaf24@2827 2 #include <public/xen.h>
kaf24@1694 3 #include <asm/page.h>
kaf24@1777 4 #include <asm/msr.h>
kaf24@1694 5
kaf24@1694 6 #define SECONDARY_CPU_FLAG 0xA5A5A5A5
kaf24@1694 7
kaf24@1777 8 .text
kaf24@1694 9 .code32
kaf24@1694 10
kaf24@1694 11 ENTRY(start)
kaf24@1694 12 jmp __start
kaf24@1750 13
kaf24@1750 14 .org 0x004
kaf24@1694 15 /*** MULTIBOOT HEADER ****/
kaf24@1694 16 /* Magic number indicating a Multiboot header. */
kaf24@1777 17 .long 0x1BADB002
kaf24@1777 18 /* Flags to bootloader (see Multiboot spec). */
kaf24@1777 19 .long 0x00000002
kaf24@1777 20 /* Checksum: must be the negated sum of the first two fields. */
kaf24@1777 21 .long -0x1BADB004
kaf24@1750 22
kaf24@1750 23 .org 0x010
kaf24@1830 24 .asciz "ERR: Not a 64-bit CPU!"
kaf24@1830 25 .org 0x028
kaf24@1830 26 .asciz "ERR: Not a Multiboot bootloader!"
kaf24@1694 27 bad_cpu:
kaf24@1750 28 mov $0x100010,%esi # Error message
kaf24@1830 29 jmp print_err
kaf24@1830 30 not_multiboot:
kaf24@1830 31 mov $0x100028,%esi # Error message
kaf24@1830 32 print_err:
kaf24@1708 33 mov $0xB8000,%edi # VGA framebuffer
kaf24@1708 34 1: mov (%esi),%bl
kaf24@1708 35 test %bl,%bl # Terminate on '\0' sentinel
kaf24@1708 36 2: je 2b
kaf24@1708 37 mov $0x3f8+5,%dx # UART Line Status Register
kaf24@1708 38 3: in %dx,%al
kaf24@1708 39 test $0x20,%al # Test THR Empty flag
kaf24@1708 40 je 3b
kaf24@1708 41 mov $0x3f8+0,%dx # UART Transmit Holding Register
kaf24@1708 42 mov %bl,%al
kaf24@1708 43 out %al,%dx # Send a character over the serial line
kaf24@1708 44 movsb # Write a character to the VGA framebuffer
kaf24@1708 45 mov $7,%al
kaf24@1708 46 stosb # Write an attribute to the VGA framebuffer
kaf24@1708 47 jmp 1b
kaf24@1694 48
kaf24@1694 49 __start:
kaf24@1750 50 cld
kaf24@1750 51 cli
kaf24@1750 52
kaf24@1694 53 /* Set up a few descriptors: on entry only CS is guaranteed good. */
kaf24@1750 54 lgdt %cs:0x1001f0
kaf24@1694 55 mov $(__HYPERVISOR_DS),%ecx
kaf24@1694 56 mov %ecx,%ds
kaf24@1694 57 mov %ecx,%es
kaf24@1694 58
kaf24@1830 59 /* Check for Multiboot bootloader */
kaf24@1830 60 cmp $(SECONDARY_CPU_FLAG),%ebx
kaf24@1830 61 je skip_multiboot_check
kaf24@1830 62 cmp $0x2BADB002,%eax
kaf24@1830 63 jne not_multiboot
kaf24@1830 64 skip_multiboot_check:
kaf24@1830 65
kaf24@1830 66 /* Save the Multiboot info structure for later use. */
kaf24@1830 67 mov %ebx,0x1001e0
kaf24@1830 68
kaf24@1708 69 /* We begin by interrogating the CPU for the presence of long mode. */
kaf24@1708 70 mov $0x80000000,%eax
kaf24@1708 71 cpuid
kaf24@1708 72 cmp $0x80000000,%eax # any function > 0x80000000?
kaf24@1708 73 jbe bad_cpu
kaf24@1708 74 mov $0x80000001,%eax
kaf24@1708 75 cpuid
kaf24@1708 76 bt $29,%edx # Long mode feature?
kaf24@1708 77 jnc bad_cpu
kaf24@1708 78
kaf24@1694 79 /* Set up FPU. */
kaf24@1694 80 fninit
kaf24@1694 81
kaf24@1750 82 /* Enable PAE in CR4. */
kaf24@1750 83 mov $0x20,%ecx # X86_CR4_PAE
kaf24@1694 84 mov %ecx,%cr4
kaf24@1750 85
kaf24@1777 86 /* Load pagetable base register. */
kaf24@1750 87 mov $0x101000,%eax /* idle_pg_table */
kaf24@1694 88 mov %eax,%cr3
kaf24@1777 89
kaf24@1777 90 /* Set up EFER (Extended Feature Enable Register). */
kaf24@1777 91 movl $MSR_EFER, %ecx
kaf24@1777 92 rdmsr
kaf24@1777 93 /* Long Mode, SYSCALL/SYSRET, No-Execute */
kaf24@1777 94 movl $(EFER_LME|EFER_SCE|EFER_NX),%eax
kaf24@1777 95 wrmsr
kaf24@1777 96
kaf24@1694 97 mov $0x80050033,%eax /* hi-to-lo: PG,AM,WP,NE,ET,MP,PE */
kaf24@1694 98 mov %eax,%cr0
kaf24@1694 99 jmp 1f
kaf24@1750 100
kaf24@1777 101 1: /* Now in compatibility mode. Long-jump into 64-bit mode. */
kaf24@1777 102 ljmp $(__HYPERVISOR_CS64),$0x100100
kaf24@1750 103
kaf24@1777 104 .code64
kaf24@1777 105 .org 0x0100
kaf24@1777 106
kaf24@1750 107 /* Install relocated selectors (FS/GS unused). */
kaf24@1750 108 lgdt gdt_descr(%rip)
kaf24@1694 109 mov $(__HYPERVISOR_DS),%ecx
kaf24@1694 110 mov %ecx,%ds
kaf24@1694 111 mov %ecx,%es
kaf24@1694 112 mov %ecx,%ss
kaf24@1750 113
kaf24@1750 114 /* Enable full CR4 features. */
kaf24@1750 115 mov mmu_cr4_features(%rip),%rcx
kaf24@1708 116 mov %rcx,%cr4
kaf24@1694 117
kaf24@1750 118 mov stack_start(%rip),%rsp
kaf24@1750 119
kaf24@1750 120 /* Reset EFLAGS (subsumes CLI and CLD). */
kaf24@1777 121 pushq $0
kaf24@1777 122 popf
kaf24@1750 123
kaf24@1777 124 /* Jump to high mappings. */
kaf24@1777 125 mov high_start(%rip),%rax
kaf24@1777 126 push %rax
kaf24@1777 127 ret
kaf24@1777 128 __high_start:
kaf24@1777 129
kaf24@1750 130 lidt idt_descr(%rip)
kaf24@1694 131
kaf24@1694 132 cmp $(SECONDARY_CPU_FLAG),%ebx
kaf24@1694 133 je start_secondary
kaf24@1694 134
kaf24@1750 135 /* Initialize BSS (no nasty surprises!) */
kaf24@1750 136 lea __bss_start(%rip),%rdi
kaf24@1750 137 lea _end(%rip),%rcx
kaf24@1750 138 sub %rdi,%rcx
kaf24@1750 139 xor %rax,%rax
kaf24@1750 140 rep stosb
kaf24@1750 141
kaf24@1750 142 /* Initialise IDT with simple error defaults. */
kaf24@1750 143 lea ignore_int(%rip),%rdx
kaf24@1750 144 mov $(__HYPERVISOR_CS64 << 16),%eax
kaf24@1750 145 mov %dx,%ax /* selector = 0x0010 = cs */
kaf24@1750 146 mov $0x8E00,%dx /* interrupt gate - dpl=0, present */
kaf24@1750 147 lea idt_table(%rip),%rdi
kaf24@1750 148 mov $256,%rcx
kaf24@1750 149 1: mov %eax,(%rdi)
kaf24@1750 150 mov %edx,4(%rdi)
kaf24@1750 151 add $8,%rdi
kaf24@1750 152 loop 1b
kaf24@1750 153
kaf24@1830 154 /* Pass off the Multiboot info structure to C land. */
kaf24@1830 155 mov 0x1001e0,%edi
kaf24@1830 156 lea start(%rip),%rax
kaf24@1830 157 sub $0x100000,%rax
kaf24@1830 158 add %rax,%rdi
kaf24@3613 159 call __start_xen
kaf24@3613 160 ud2 /* Force a panic (invalid opcode). */
kaf24@1694 161
kaf24@1694 162 /* This is the default interrupt handler. */
kaf24@1694 163 int_msg:
kaf24@1694 164 .asciz "Unknown interrupt\n"
kaf24@1694 165 ignore_int:
kaf24@1694 166 cld
kaf24@1694 167 mov $(__HYPERVISOR_DS),%eax
kaf24@1694 168 mov %eax,%ds
kaf24@1694 169 mov %eax,%es
kaf24@1830 170 lea int_msg(%rip),%rdi
kaf24@1694 171 call SYMBOL_NAME(printf)
kaf24@1694 172 1: jmp 1b
kaf24@1694 173
kaf24@1708 174 .code32
kaf24@1777 175
kaf24@1777 176 .org 0x1e0
kaf24@1777 177
kaf24@1694 178 /*** DESCRIPTOR TABLES ***/
kaf24@1694 179
kaf24@1694 180 .globl SYMBOL_NAME(idt)
kaf24@1694 181 .globl SYMBOL_NAME(gdt)
kaf24@1694 182
kaf24@1750 183 .org 0x1f0
kaf24@1694 184 .word (LAST_RESERVED_GDT_ENTRY*8)+7
kaf24@1750 185 .long 0x100200 # gdt_table
kaf24@1694 186
kaf24@1750 187 .org 0x200
kaf24@1694 188 ENTRY(gdt_table)
kaf24@1694 189 .fill FIRST_RESERVED_GDT_ENTRY,8,0
kaf24@1694 190 .quad 0x0000000000000000 /* unused */
kaf24@1694 191 .quad 0x00cf9a000000ffff /* 0x0808 ring 0 code, compatability */
kaf24@1694 192 .quad 0x00af9a000000ffff /* 0x0810 ring 0 code, 64-bit mode */
kaf24@1694 193 .quad 0x00cf92000000ffff /* 0x0818 ring 0 data */
kaf24@1694 194 .quad 0x00cffa000000ffff /* 0x0823 ring 3 code, compatibility */
kaf24@1694 195 .quad 0x00affa000000ffff /* 0x082b ring 3 code, 64-bit mode */
kaf24@1694 196 .quad 0x00cff2000000ffff /* 0x0833 ring 3 data */
kaf24@1750 197 .quad 0x0000000000000000 /* unused */
kaf24@1750 198 .fill 2*NR_CPUS,8,0 /* space for TSS and LDT per CPU */
kaf24@1750 199
kaf24@1750 200 .word 0
kaf24@1750 201 gdt_descr:
kaf24@1777 202 .word (LAST_RESERVED_GDT_ENTRY*8)+7
kaf24@1750 203 SYMBOL_NAME(gdt):
kaf24@1750 204 .quad SYMBOL_NAME(gdt_table)
kaf24@1694 205
kaf24@1750 206 .word 0
kaf24@1750 207 idt_descr:
kaf24@1777 208 .word 256*8-1
kaf24@1750 209 SYMBOL_NAME(idt):
kaf24@1777 210 .quad SYMBOL_NAME(idt_table)
kaf24@1750 211
kaf24@1750 212 ENTRY(stack_start)
kaf24@1777 213 .quad SYMBOL_NAME(cpu0_stack) + 8100
kaf24@1750 214
kaf24@1750 215 high_start:
kaf24@1750 216 .quad __high_start
kaf24@1750 217
kaf24@1777 218 /* Initial PML4 -- level-4 page table */
kaf24@1694 219 .org 0x1000
kaf24@1750 220 ENTRY(idle_pg_table)
kaf24@1750 221 ENTRY(idle_pg_table_4)
kaf24@1750 222 .quad 0x0000000000102007 # PML4[0]
kaf24@1750 223 .fill 261,8,0
kaf24@1750 224 .quad 0x0000000000102007 # PML4[262]
kaf24@1777 225
kaf24@1777 226 /* Initial PDP -- level-3 page table */
kaf24@1694 227 .org 0x2000
kaf24@1750 228 ENTRY(idle_pg_table_l3)
kaf24@1750 229 .quad 0x0000000000103007
kaf24@1777 230
kaf24@1777 231 /* Initial PDE -- level-2 page table. */
kaf24@1750 232 .org 0x3000
kaf24@1750 233 ENTRY(idle_pg_table_l2)
kaf24@1777 234 .macro identmap from=0, count=512
kaf24@1777 235 .if \count-1
kaf24@1777 236 identmap "(\from+0)","(\count/2)"
kaf24@1777 237 identmap "(\from+(0x200000*(\count/2)))","(\count/2)"
kaf24@1777 238 .else
kaf24@1777 239 .quad 0x00000000000001e3 + \from
kaf24@1777 240 .endif
kaf24@1777 241 .endm
kaf24@1777 242 identmap /* Too orangey for crows :-) */
kaf24@1777 243
kaf24@1750 244 .org 0x4000
kaf24@1694 245 ENTRY(cpu0_stack) # Initial stack is 8kB
kaf24@1777 246
kaf24@1750 247 .org 0x6000
kaf24@1694 248 ENTRY(stext)
kaf24@1694 249 ENTRY(_stext)
kaf24@1750 250
kaf24@3632 251 .globl ret_from_intr, copy_to_user, set_intr_gate, die
kaf24@1750 252 ret_from_intr:
kaf24@1750 253 copy_to_user:
kaf24@1750 254 set_intr_gate:
kaf24@1750 255 die:
kaf24@3314 256 .globl copy_from_user, show_registers, do_iopl
kaf24@1750 257 copy_from_user:
kaf24@1750 258 show_registers:
kaf24@1750 259 do_iopl:
kaf24@3613 260 .globl idt_table, copy_user_generic, idt_tables, new_thread
kaf24@1750 261 idt_table:
kaf24@1750 262 copy_user_generic:
kaf24@1750 263 idt_tables:
kaf24@1750 264 new_thread:
kaf24@3314 265 .globl switch_to, __get_user_1, __get_user_4, __get_user_8, trap_init
kaf24@1750 266 switch_to:
kaf24@1750 267 __get_user_1:
kaf24@3314 268 __get_user_4:
kaf24@1750 269 __get_user_8:
kaf24@3314 270 trap_init:
kaf24@3314 271 .globl set_debugreg
kaf24@1750 272 set_debugreg:
kaf24@1750 273