ia32_support.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * IA32 helper functions
  3. *
  4. * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
  5. * Copyright (C) 2000 Asit K. Mallick <asit.k.mallick@intel.com>
  6. * Copyright (C) 2001-2002 Hewlett-Packard Co
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. *
  9. * 06/16/00 A. Mallick added csd/ssd/tssd for ia32 thread context
  10. * 02/19/01 D. Mosberger dropped tssd; it's not needed
  11. * 09/14/01 D. Mosberger fixed memory management for gdt/tss page
  12. * 09/29/01 D. Mosberger added ia32_load_segment_descriptors()
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/mm.h>
  17. #include <linux/sched.h>
  18. #include <asm/intrinsics.h>
  19. #include <asm/page.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/system.h>
  22. #include <asm/processor.h>
  23. #include <asm/uaccess.h>
  24. #include "ia32priv.h"
  25. extern int die_if_kernel (char *str, struct pt_regs *regs, long err);
  26. struct page *ia32_shared_page[NR_CPUS];
  27. unsigned long *ia32_boot_gdt;
  28. unsigned long *cpu_gdt_table[NR_CPUS];
  29. struct page *ia32_gate_page;
  30. static unsigned long
  31. load_desc (u16 selector)
  32. {
  33. unsigned long *table, limit, index;
  34. if (!selector)
  35. return 0;
  36. if (selector & IA32_SEGSEL_TI) {
  37. table = (unsigned long *) IA32_LDT_OFFSET;
  38. limit = IA32_LDT_ENTRIES;
  39. } else {
  40. table = cpu_gdt_table[smp_processor_id()];
  41. limit = IA32_PAGE_SIZE / sizeof(ia32_boot_gdt[0]);
  42. }
  43. index = selector >> IA32_SEGSEL_INDEX_SHIFT;
  44. if (index >= limit)
  45. return 0;
  46. return IA32_SEG_UNSCRAMBLE(table[index]);
  47. }
  48. void
  49. ia32_load_segment_descriptors (struct task_struct *task)
  50. {
  51. struct pt_regs *regs = task_pt_regs(task);
  52. /* Setup the segment descriptors */
  53. regs->r24 = load_desc(regs->r16 >> 16); /* ESD */
  54. regs->r27 = load_desc(regs->r16 >> 0); /* DSD */
  55. regs->r28 = load_desc(regs->r16 >> 32); /* FSD */
  56. regs->r29 = load_desc(regs->r16 >> 48); /* GSD */
  57. regs->ar_csd = load_desc(regs->r17 >> 0); /* CSD */
  58. regs->ar_ssd = load_desc(regs->r17 >> 16); /* SSD */
  59. }
  60. int
  61. ia32_clone_tls (struct task_struct *child, struct pt_regs *childregs)
  62. {
  63. struct desc_struct *desc;
  64. struct ia32_user_desc info;
  65. int idx;
  66. if (copy_from_user(&info, (void __user *)(childregs->r14 & 0xffffffff), sizeof(info)))
  67. return -EFAULT;
  68. if (LDT_empty(&info))
  69. return -EINVAL;
  70. idx = info.entry_number;
  71. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  72. return -EINVAL;
  73. desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
  74. desc->a = LDT_entry_a(&info);
  75. desc->b = LDT_entry_b(&info);
  76. /* XXX: can this be done in a cleaner way ? */
  77. load_TLS(&child->thread, smp_processor_id());
  78. ia32_load_segment_descriptors(child);
  79. load_TLS(&current->thread, smp_processor_id());
  80. return 0;
  81. }
  82. void
  83. ia32_save_state (struct task_struct *t)
  84. {
  85. t->thread.eflag = ia64_getreg(_IA64_REG_AR_EFLAG);
  86. t->thread.fsr = ia64_getreg(_IA64_REG_AR_FSR);
  87. t->thread.fcr = ia64_getreg(_IA64_REG_AR_FCR);
  88. t->thread.fir = ia64_getreg(_IA64_REG_AR_FIR);
  89. t->thread.fdr = ia64_getreg(_IA64_REG_AR_FDR);
  90. ia64_set_kr(IA64_KR_IO_BASE, t->thread.old_iob);
  91. ia64_set_kr(IA64_KR_TSSD, t->thread.old_k1);
  92. }
  93. void
  94. ia32_load_state (struct task_struct *t)
  95. {
  96. unsigned long eflag, fsr, fcr, fir, fdr, tssd;
  97. struct pt_regs *regs = task_pt_regs(t);
  98. eflag = t->thread.eflag;
  99. fsr = t->thread.fsr;
  100. fcr = t->thread.fcr;
  101. fir = t->thread.fir;
  102. fdr = t->thread.fdr;
  103. tssd = load_desc(_TSS); /* TSSD */
  104. ia64_setreg(_IA64_REG_AR_EFLAG, eflag);
  105. ia64_setreg(_IA64_REG_AR_FSR, fsr);
  106. ia64_setreg(_IA64_REG_AR_FCR, fcr);
  107. ia64_setreg(_IA64_REG_AR_FIR, fir);
  108. ia64_setreg(_IA64_REG_AR_FDR, fdr);
  109. current->thread.old_iob = ia64_get_kr(IA64_KR_IO_BASE);
  110. current->thread.old_k1 = ia64_get_kr(IA64_KR_TSSD);
  111. ia64_set_kr(IA64_KR_IO_BASE, IA32_IOBASE);
  112. ia64_set_kr(IA64_KR_TSSD, tssd);
  113. regs->r17 = (_TSS << 48) | (_LDT << 32) | (__u32) regs->r17;
  114. regs->r30 = load_desc(_LDT); /* LDTD */
  115. load_TLS(&t->thread, smp_processor_id());
  116. }
  117. /*
  118. * Setup IA32 GDT and TSS
  119. */
  120. void
  121. ia32_gdt_init (void)
  122. {
  123. int cpu = smp_processor_id();
  124. ia32_shared_page[cpu] = alloc_page(GFP_KERNEL);
  125. if (!ia32_shared_page[cpu])
  126. panic("failed to allocate ia32_shared_page[%d]\n", cpu);
  127. cpu_gdt_table[cpu] = page_address(ia32_shared_page[cpu]);
  128. /* Copy from the boot cpu's GDT */
  129. memcpy(cpu_gdt_table[cpu], ia32_boot_gdt, PAGE_SIZE);
  130. }
  131. /*
  132. * Setup IA32 GDT and TSS
  133. */
  134. static void
  135. ia32_boot_gdt_init (void)
  136. {
  137. unsigned long ldt_size;
  138. ia32_shared_page[0] = alloc_page(GFP_KERNEL);
  139. if (!ia32_shared_page[0])
  140. panic("failed to allocate ia32_shared_page[0]\n");
  141. ia32_boot_gdt = page_address(ia32_shared_page[0]);
  142. cpu_gdt_table[0] = ia32_boot_gdt;
  143. /* CS descriptor in IA-32 (scrambled) format */
  144. ia32_boot_gdt[__USER_CS >> 3]
  145. = IA32_SEG_DESCRIPTOR(0, (IA32_GATE_END-1) >> IA32_PAGE_SHIFT,
  146. 0xb, 1, 3, 1, 1, 1, 1);
  147. /* DS descriptor in IA-32 (scrambled) format */
  148. ia32_boot_gdt[__USER_DS >> 3]
  149. = IA32_SEG_DESCRIPTOR(0, (IA32_GATE_END-1) >> IA32_PAGE_SHIFT,
  150. 0x3, 1, 3, 1, 1, 1, 1);
  151. ldt_size = PAGE_ALIGN(IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE);
  152. ia32_boot_gdt[TSS_ENTRY] = IA32_SEG_DESCRIPTOR(IA32_TSS_OFFSET, 235,
  153. 0xb, 0, 3, 1, 1, 1, 0);
  154. ia32_boot_gdt[LDT_ENTRY] = IA32_SEG_DESCRIPTOR(IA32_LDT_OFFSET, ldt_size - 1,
  155. 0x2, 0, 3, 1, 1, 1, 0);
  156. }
  157. static void
  158. ia32_gate_page_init(void)
  159. {
  160. unsigned long *sr;
  161. ia32_gate_page = alloc_page(GFP_KERNEL);
  162. sr = page_address(ia32_gate_page);
  163. /* This is popl %eax ; movl $,%eax ; int $0x80 */
  164. *sr++ = 0xb858 | (__IA32_NR_sigreturn << 16) | (0x80cdUL << 48);
  165. /* This is movl $,%eax ; int $0x80 */
  166. *sr = 0xb8 | (__IA32_NR_rt_sigreturn << 8) | (0x80cdUL << 40);
  167. }
  168. void
  169. ia32_mem_init(void)
  170. {
  171. ia32_boot_gdt_init();
  172. ia32_gate_page_init();
  173. }
  174. /*
  175. * Handle bad IA32 interrupt via syscall
  176. */
  177. void
  178. ia32_bad_interrupt (unsigned long int_num, struct pt_regs *regs)
  179. {
  180. siginfo_t siginfo;
  181. if (die_if_kernel("Bad IA-32 interrupt", regs, int_num))
  182. return;
  183. siginfo.si_signo = SIGTRAP;
  184. siginfo.si_errno = int_num; /* XXX is it OK to abuse si_errno like this? */
  185. siginfo.si_flags = 0;
  186. siginfo.si_isr = 0;
  187. siginfo.si_addr = NULL;
  188. siginfo.si_imm = 0;
  189. siginfo.si_code = TRAP_BRKPT;
  190. force_sig_info(SIGTRAP, &siginfo, current);
  191. }
  192. void
  193. ia32_cpu_init (void)
  194. {
  195. /* initialize global ia32 state - CR0 and CR4 */
  196. ia64_setreg(_IA64_REG_AR_CFLAG, (((ulong) IA32_CR4 << 32) | IA32_CR0));
  197. }
  198. static int __init
  199. ia32_init (void)
  200. {
  201. #if PAGE_SHIFT > IA32_PAGE_SHIFT
  202. {
  203. extern struct kmem_cache *ia64_partial_page_cachep;
  204. ia64_partial_page_cachep = kmem_cache_create("ia64_partial_page_cache",
  205. sizeof(struct ia64_partial_page),
  206. 0, SLAB_PANIC, NULL);
  207. }
  208. #endif
  209. return 0;
  210. }
  211. __initcall(ia32_init);