VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / ia64 / ia32 / ia32_support.c
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
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/mm.h>
18 #include <linux/personality.h>
19 #include <linux/sched.h>
20
21 #include <asm/intrinsics.h>
22 #include <asm/page.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/processor.h>
26 #include <asm/uaccess.h>
27
28 #include "ia32priv.h"
29
30 extern void die_if_kernel (char *str, struct pt_regs *regs, long err);
31
32 struct exec_domain ia32_exec_domain;
33 struct page *ia32_shared_page[NR_CPUS];
34 unsigned long *ia32_boot_gdt;
35 unsigned long *cpu_gdt_table[NR_CPUS];
36
37 static unsigned long
38 load_desc (u16 selector)
39 {
40         unsigned long *table, limit, index;
41
42         if (!selector)
43                 return 0;
44         if (selector & IA32_SEGSEL_TI) {
45                 table = (unsigned long *) IA32_LDT_OFFSET;
46                 limit = IA32_LDT_ENTRIES;
47         } else {
48                 table = cpu_gdt_table[smp_processor_id()];
49                 limit = IA32_PAGE_SIZE / sizeof(ia32_boot_gdt[0]);
50         }
51         index = selector >> IA32_SEGSEL_INDEX_SHIFT;
52         if (index >= limit)
53                 return 0;
54         return IA32_SEG_UNSCRAMBLE(table[index]);
55 }
56
57 void
58 ia32_load_segment_descriptors (struct task_struct *task)
59 {
60         struct pt_regs *regs = ia64_task_regs(task);
61
62         /* Setup the segment descriptors */
63         regs->r24 = load_desc(regs->r16 >> 16);         /* ESD */
64         regs->r27 = load_desc(regs->r16 >>  0);         /* DSD */
65         regs->r28 = load_desc(regs->r16 >> 32);         /* FSD */
66         regs->r29 = load_desc(regs->r16 >> 48);         /* GSD */
67         regs->ar_csd = load_desc(regs->r17 >>  0);      /* CSD */
68         regs->ar_ssd = load_desc(regs->r17 >> 16);      /* SSD */
69 }
70
71 int
72 ia32_clone_tls (struct task_struct *child, struct pt_regs *childregs)
73 {
74         struct desc_struct *desc;
75         struct ia32_user_desc info;
76         int idx;
77
78         if (copy_from_user(&info, (void *)(childregs->r14 & 0xffffffff), sizeof(info)))
79                 return -EFAULT;
80         if (LDT_empty(&info))
81                 return -EINVAL;
82
83         idx = info.entry_number;
84         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
85                 return -EINVAL;
86
87         desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
88         desc->a = LDT_entry_a(&info);
89         desc->b = LDT_entry_b(&info);
90
91         /* XXX: can this be done in a cleaner way ? */
92         load_TLS(&child->thread, smp_processor_id());
93         ia32_load_segment_descriptors(child);
94         load_TLS(&current->thread, smp_processor_id());
95
96         return 0;
97 }
98
99 void
100 ia32_save_state (struct task_struct *t)
101 {
102         t->thread.eflag = ia64_getreg(_IA64_REG_AR_EFLAG);
103         t->thread.fsr   = ia64_getreg(_IA64_REG_AR_FSR);
104         t->thread.fcr   = ia64_getreg(_IA64_REG_AR_FCR);
105         t->thread.fir   = ia64_getreg(_IA64_REG_AR_FIR);
106         t->thread.fdr   = ia64_getreg(_IA64_REG_AR_FDR);
107         ia64_set_kr(IA64_KR_IO_BASE, t->thread.old_iob);
108         ia64_set_kr(IA64_KR_TSSD, t->thread.old_k1);
109 }
110
111 void
112 ia32_load_state (struct task_struct *t)
113 {
114         unsigned long eflag, fsr, fcr, fir, fdr, tssd;
115         struct pt_regs *regs = ia64_task_regs(t);
116
117         eflag = t->thread.eflag;
118         fsr = t->thread.fsr;
119         fcr = t->thread.fcr;
120         fir = t->thread.fir;
121         fdr = t->thread.fdr;
122         tssd = load_desc(_TSS);                                 /* TSSD */
123
124         ia64_setreg(_IA64_REG_AR_EFLAG, eflag);
125         ia64_setreg(_IA64_REG_AR_FSR, fsr);
126         ia64_setreg(_IA64_REG_AR_FCR, fcr);
127         ia64_setreg(_IA64_REG_AR_FIR, fir);
128         ia64_setreg(_IA64_REG_AR_FDR, fdr);
129         current->thread.old_iob = ia64_get_kr(IA64_KR_IO_BASE);
130         current->thread.old_k1 = ia64_get_kr(IA64_KR_TSSD);
131         ia64_set_kr(IA64_KR_IO_BASE, IA32_IOBASE);
132         ia64_set_kr(IA64_KR_TSSD, tssd);
133
134         regs->r17 = (_TSS << 48) | (_LDT << 32) | (__u32) regs->r17;
135         regs->r30 = load_desc(_LDT);                            /* LDTD */
136         load_TLS(&t->thread, smp_processor_id());
137 }
138
139 /*
140  * Setup IA32 GDT and TSS
141  */
142 void
143 ia32_gdt_init (void)
144 {
145         int cpu = smp_processor_id();
146
147         ia32_shared_page[cpu] = alloc_page(GFP_KERNEL);
148         cpu_gdt_table[cpu] = page_address(ia32_shared_page[cpu]);
149
150         /* Copy from the boot cpu's GDT */
151         memcpy(cpu_gdt_table[cpu], ia32_boot_gdt, PAGE_SIZE);
152 }
153
154
155 /*
156  * Setup IA32 GDT and TSS
157  */
158 void
159 ia32_boot_gdt_init (void)
160 {
161         unsigned long ldt_size;
162
163         ia32_shared_page[0] = alloc_page(GFP_KERNEL);
164         ia32_boot_gdt = page_address(ia32_shared_page[0]);
165         cpu_gdt_table[0] = ia32_boot_gdt;
166
167         /* CS descriptor in IA-32 (scrambled) format */
168         ia32_boot_gdt[__USER_CS >> 3]
169                 = IA32_SEG_DESCRIPTOR(0, (IA32_PAGE_OFFSET-1) >> IA32_PAGE_SHIFT,
170                                       0xb, 1, 3, 1, 1, 1, 1);
171
172         /* DS descriptor in IA-32 (scrambled) format */
173         ia32_boot_gdt[__USER_DS >> 3]
174                 = IA32_SEG_DESCRIPTOR(0, (IA32_PAGE_OFFSET-1) >> IA32_PAGE_SHIFT,
175                                       0x3, 1, 3, 1, 1, 1, 1);
176
177         ldt_size = PAGE_ALIGN(IA32_LDT_ENTRIES*IA32_LDT_ENTRY_SIZE);
178         ia32_boot_gdt[TSS_ENTRY] = IA32_SEG_DESCRIPTOR(IA32_TSS_OFFSET, 235,
179                                                        0xb, 0, 3, 1, 1, 1, 0);
180         ia32_boot_gdt[LDT_ENTRY] = IA32_SEG_DESCRIPTOR(IA32_LDT_OFFSET, ldt_size - 1,
181                                                        0x2, 0, 3, 1, 1, 1, 0);
182 }
183
184 /*
185  * Handle bad IA32 interrupt via syscall
186  */
187 void
188 ia32_bad_interrupt (unsigned long int_num, struct pt_regs *regs)
189 {
190         siginfo_t siginfo;
191
192         die_if_kernel("Bad IA-32 interrupt", regs, int_num);
193
194         siginfo.si_signo = SIGTRAP;
195         siginfo.si_errno = int_num;     /* XXX is it OK to abuse si_errno like this? */
196         siginfo.si_flags = 0;
197         siginfo.si_isr = 0;
198         siginfo.si_addr = 0;
199         siginfo.si_imm = 0;
200         siginfo.si_code = TRAP_BRKPT;
201         force_sig_info(SIGTRAP, &siginfo, current);
202 }
203
204 void
205 ia32_cpu_init (void)
206 {
207         /* initialize global ia32 state - CR0 and CR4 */
208         ia64_setreg(_IA64_REG_AR_CFLAG, (((ulong) IA32_CR4 << 32) | IA32_CR0));
209 }
210
211 static int __init
212 ia32_init (void)
213 {
214         ia32_exec_domain.name = "Linux/x86";
215         ia32_exec_domain.handler = NULL;
216         ia32_exec_domain.pers_low = PER_LINUX32;
217         ia32_exec_domain.pers_high = PER_LINUX32;
218         ia32_exec_domain.signal_map = default_exec_domain.signal_map;
219         ia32_exec_domain.signal_invmap = default_exec_domain.signal_invmap;
220         register_exec_domain(&ia32_exec_domain);
221
222 #if PAGE_SHIFT > IA32_PAGE_SHIFT
223         {
224                 extern kmem_cache_t *partial_page_cachep;
225
226                 partial_page_cachep = kmem_cache_create("partial_page_cache",
227                                                         sizeof(struct partial_page), 0, 0,
228                                                         NULL, NULL);
229                 if (!partial_page_cachep)
230                         panic("Cannot create partial page SLAB cache");
231         }
232 #endif
233         return 0;
234 }
235
236 __initcall(ia32_init);