setup64.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* 
  2.  * X86-64 specific CPU setup.
  3.  * Copyright (C) 1995  Linus Torvalds
  4.  * Copyright 2001, 2002 SuSE Labs / Andi Kleen.
  5.  * See setup.c for older changelog.
  6.  * $Id: setup64.c,v 1.15 2002/09/05 15:25:43 ak Exp $
  7.  */ 
  8. #include <linux/config.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/string.h>
  13. #include <asm/pda.h>
  14. #include <asm/pda.h>
  15. #include <asm/processor.h>
  16. #include <asm/desc.h>
  17. #include <asm/bitops.h>
  18. #include <asm/atomic.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/proto.h>
  21. char x86_boot_params[2048] __initdata = {0,};
  22. static unsigned long cpu_initialized __initdata = 0;
  23. struct x8664_pda cpu_pda[NR_CPUS] __cacheline_aligned; 
  24. extern void system_call(void); 
  25. extern void ia32_cstar_target(void); 
  26. struct desc_ptr gdt_descr = { 0 /* filled in */, (unsigned long) gdt_table }; 
  27. struct desc_ptr idt_descr = { 256 * 16, (unsigned long) idt_table }; 
  28. unsigned long __supported_pte_mask = ~_PAGE_NX; 
  29. static int do_not_nx = 1; 
  30. char boot_cpu_stack[IRQSTACKSIZE] __cacheline_aligned;
  31. static int __init nonx_setup(char *str)
  32. {
  33. if (strstr(str,"off")) { 
  34. __supported_pte_mask &= ~_PAGE_NX; 
  35. do_not_nx = 1; 
  36. } else if (strstr(str, "on")) { 
  37. do_not_nx = 0; 
  38. __supported_pte_mask |= _PAGE_NX; 
  39. return 1;
  40. __setup("noexec=", nonx_setup); 
  41. void pda_init(int cpu)
  42.         pml4_t *level4;
  43. if (cpu == 0) {
  44. /* others are initialized in smpboot.c */
  45. cpu_pda[cpu].pcurrent = init_tasks[cpu];
  46. cpu_pda[cpu].irqstackptr = boot_cpu_stack; 
  47. level4 = init_level4_pgt; 
  48. } else {
  49. cpu_pda[cpu].irqstackptr = (char *)
  50. __get_free_pages(GFP_ATOMIC, IRQSTACK_ORDER);
  51. if (!cpu_pda[cpu].irqstackptr)
  52. panic("cannot allocate irqstack for cpu %dn", cpu); 
  53. level4 = (pml4_t *)__get_free_pages(GFP_ATOMIC, 0); 
  54. }   
  55. if (!level4) 
  56. panic("Cannot allocate top level page for cpu %d", cpu); 
  57. cpu_pda[cpu].level4_pgt = (unsigned long *)level4; 
  58. if (level4 != init_level4_pgt)
  59. memcpy(level4, &init_level4_pgt, PAGE_SIZE); 
  60. set_pml4(level4 + 510, 
  61.  mk_kernel_pml4(__pa_symbol(boot_vmalloc_pgt), KERNPG_TABLE));
  62. asm volatile("movq %0,%%cr3" :: "r" (__pa(level4))); 
  63. cpu_pda[cpu].irqstackptr += IRQSTACKSIZE-64;
  64. cpu_pda[cpu].cpunumber = cpu; 
  65. cpu_pda[cpu].irqcount = -1;
  66. asm volatile("movl %0,%%fs ; movl %0,%%gs" :: "r" (0)); 
  67. wrmsrl(MSR_GS_BASE, cpu_pda + cpu);
  68. #define EXCEPTION_STK_ORDER 0 /* >= N_EXCEPTION_STACKS*EXCEPTION_STKSZ */
  69. char boot_exception_stacks[N_EXCEPTION_STACKS*EXCEPTION_STKSZ];
  70. /*
  71.  * cpu_init() initializes state that is per-CPU. Some data is already
  72.  * initialized (naturally) in the bootstrap process, such as the GDT
  73.  * and IDT. We reload them nevertheless, this function acts as a
  74.  * 'CPU state barrier', nothing should get across.
  75.  * A lot of state is already set up in PDA init.
  76.  */
  77. void __init cpu_init (void)
  78. {
  79. #ifdef CONFIG_SMP
  80. int nr = stack_smp_processor_id();
  81. #else
  82. int nr = smp_processor_id();
  83. #endif
  84. struct tss_struct * t = &init_tss[nr];
  85. unsigned long v, efer; 
  86. char *estacks; 
  87. /* CPU 0 is initialised in head64.c */
  88. if (nr != 0) {
  89. estacks = (char *)__get_free_pages(GFP_ATOMIC, 0); 
  90. if (!estacks)
  91. panic("Can't allocate exception stacks for CPU %dn",nr);
  92. pda_init(nr);  
  93. } else 
  94. estacks = boot_exception_stacks; 
  95. if (test_and_set_bit(nr, &cpu_initialized))
  96. panic("CPU#%d already initialized!n", nr);
  97. printk("Initializing CPU#%dn", nr);
  98. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  99. gdt_descr.size = NR_CPUS * sizeof(struct per_cpu_gdt) + __GDT_HEAD_SIZE; 
  100. __asm__ __volatile__("lgdt %0": "=m" (gdt_descr));
  101. __asm__ __volatile__("lidt %0": "=m" (idt_descr));
  102. /*
  103.  * Delete NT
  104.  */
  105. asm volatile("pushfq ; popq %%rax ; btr $14,%%rax ; pushq %%rax ; popfq" ::: "eax");
  106. /* 
  107.  * LSTAR and STAR live in a bit strange symbiosis.
  108.  * They both write to the same internal register. STAR allows to set CS/DS
  109.  * but only a 32bit target. LSTAR sets the 64bit rip.   
  110.  */ 
  111. wrmsrl(MSR_STAR,  ((u64)__USER32_CS)<<48  | ((u64)__KERNEL_CS)<<32); 
  112. wrmsrl(MSR_LSTAR, system_call); 
  113. #ifdef CONFIG_IA32_EMULATION   
  114. wrmsrl(MSR_CSTAR, ia32_cstar_target); 
  115. #endif
  116. if (!do_not_nx) { 
  117. rdmsrl(MSR_EFER, efer); 
  118. if (!(efer & EFER_NX)) { 
  119. __supported_pte_mask &= ~_PAGE_NX; 
  120. }
  121. t->io_map_base = INVALID_IO_BITMAP_OFFSET;
  122. memset(t->io_bitmap, 0xff, sizeof(t->io_bitmap));
  123. /* Flags to clear on syscall */
  124. wrmsrl(MSR_SYSCALL_MASK, EF_TF|EF_DF|EF_IE); 
  125. wrmsrl(MSR_FS_BASE, 0);
  126. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  127. barrier(); 
  128. /*
  129.  * set up and load the per-CPU TSS
  130.  */
  131. estacks += EXCEPTION_STKSZ;
  132. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  133. t->ist[v] = (unsigned long)estacks;
  134. estacks += EXCEPTION_STKSZ;
  135. }
  136. atomic_inc(&init_mm.mm_count);
  137. current->active_mm = &init_mm;
  138. if(current->mm)
  139. BUG();
  140. enter_lazy_tlb(&init_mm, current, nr);
  141. set_tss_desc(nr, t);
  142. load_TR(nr);
  143. load_LDT(&init_mm);
  144. /*
  145.  * Clear all 6 debug registers:
  146.  */
  147. set_debug(0UL, 0);
  148. set_debug(0UL, 1);
  149. set_debug(0UL, 2);
  150. set_debug(0UL, 3);
  151. set_debug(0UL, 6);
  152. set_debug(0UL, 7);
  153. /*
  154.  * Force FPU initialization:
  155.  */
  156. current->flags &= ~PF_USEDFPU;
  157. current->used_math = 0;
  158. stts();
  159. }