vserver 1.9.5.x5
[linux-2.6.git] / arch / ia64 / ia32 / ia32priv.h
1 #ifndef _ASM_IA64_IA32_PRIV_H
2 #define _ASM_IA64_IA32_PRIV_H
3
4 #include <linux/config.h>
5
6 #include <asm/ia32.h>
7
8 #ifdef CONFIG_IA32_SUPPORT
9
10 #include <linux/binfmts.h>
11 #include <linux/compat.h>
12 #include <linux/rbtree.h>
13
14 #include <asm/processor.h>
15
16 /*
17  * 32 bit structures for IA32 support.
18  */
19
20 #define IA32_PAGE_SIZE          (1UL << IA32_PAGE_SHIFT)
21 #define IA32_PAGE_MASK          (~(IA32_PAGE_SIZE - 1))
22 #define IA32_PAGE_ALIGN(addr)   (((addr) + IA32_PAGE_SIZE - 1) & IA32_PAGE_MASK)
23 #define IA32_CLOCKS_PER_SEC     100     /* Cast in stone for IA32 Linux */
24
25 /*
26  * partially mapped pages provide precise accounting of which 4k sub pages
27  * are mapped and which ones are not, thereby improving IA-32 compatibility.
28  */
29 struct partial_page {
30         struct partial_page     *next; /* linked list, sorted by address */
31         struct rb_node          pp_rb;
32         /* 64K is the largest "normal" page supported by ia64 ABI. So 4K*32
33          * should suffice.*/
34         unsigned int            bitmap;
35         unsigned int            base;
36 };
37
38 struct partial_page_list {
39         struct partial_page     *pp_head; /* list head, points to the lowest
40                                            * addressed partial page */
41         struct rb_root          ppl_rb;
42         struct partial_page     *pp_hint; /* pp_hint->next is the last
43                                            * accessed partial page */
44         atomic_t                pp_count; /* reference count */
45 };
46
47 #if PAGE_SHIFT > IA32_PAGE_SHIFT
48 struct partial_page_list* ia32_init_pp_list (void);
49 #else
50 # define ia32_init_pp_list()    0
51 #endif
52
53 /* sigcontext.h */
54 /*
55  * As documented in the iBCS2 standard..
56  *
57  * The first part of "struct _fpstate" is just the
58  * normal i387 hardware setup, the extra "status"
59  * word is used to save the coprocessor status word
60  * before entering the handler.
61  */
62 struct _fpreg_ia32 {
63        unsigned short significand[4];
64        unsigned short exponent;
65 };
66
67 struct _fpxreg_ia32 {
68         unsigned short significand[4];
69         unsigned short exponent;
70         unsigned short padding[3];
71 };
72
73 struct _xmmreg_ia32 {
74         unsigned int element[4];
75 };
76
77
78 struct _fpstate_ia32 {
79        unsigned int    cw,
80                        sw,
81                        tag,
82                        ipoff,
83                        cssel,
84                        dataoff,
85                        datasel;
86        struct _fpreg_ia32      _st[8];
87        unsigned short  status;
88        unsigned short  magic;          /* 0xffff = regular FPU data only */
89
90        /* FXSR FPU environment */
91        unsigned int         _fxsr_env[6];   /* FXSR FPU env is ignored */
92        unsigned int         mxcsr;
93        unsigned int         reserved;
94        struct _fpxreg_ia32  _fxsr_st[8];    /* FXSR FPU reg data is ignored */
95        struct _xmmreg_ia32  _xmm[8];
96        unsigned int         padding[56];
97 };
98
99 struct sigcontext_ia32 {
100        unsigned short gs, __gsh;
101        unsigned short fs, __fsh;
102        unsigned short es, __esh;
103        unsigned short ds, __dsh;
104        unsigned int edi;
105        unsigned int esi;
106        unsigned int ebp;
107        unsigned int esp;
108        unsigned int ebx;
109        unsigned int edx;
110        unsigned int ecx;
111        unsigned int eax;
112        unsigned int trapno;
113        unsigned int err;
114        unsigned int eip;
115        unsigned short cs, __csh;
116        unsigned int eflags;
117        unsigned int esp_at_signal;
118        unsigned short ss, __ssh;
119        unsigned int fpstate;            /* really (struct _fpstate_ia32 *) */
120        unsigned int oldmask;
121        unsigned int cr2;
122 };
123
124 /* user.h */
125 /*
126  * IA32 (Pentium III/4) FXSR, SSE support
127  *
128  * Provide support for the GDB 5.0+ PTRACE_{GET|SET}FPXREGS requests for
129  * interacting with the FXSR-format floating point environment.  Floating
130  * point data can be accessed in the regular format in the usual manner,
131  * and both the standard and SIMD floating point data can be accessed via
132  * the new ptrace requests.  In either case, changes to the FPU environment
133  * will be reflected in the task's state as expected.
134  */
135 struct ia32_user_i387_struct {
136         int     cwd;
137         int     swd;
138         int     twd;
139         int     fip;
140         int     fcs;
141         int     foo;
142         int     fos;
143         /* 8*10 bytes for each FP-reg = 80 bytes */
144         struct _fpreg_ia32      st_space[8];
145 };
146
147 struct ia32_user_fxsr_struct {
148         unsigned short  cwd;
149         unsigned short  swd;
150         unsigned short  twd;
151         unsigned short  fop;
152         int     fip;
153         int     fcs;
154         int     foo;
155         int     fos;
156         int     mxcsr;
157         int     reserved;
158         int     st_space[32];   /* 8*16 bytes for each FP-reg = 128 bytes */
159         int     xmm_space[32];  /* 8*16 bytes for each XMM-reg = 128 bytes */
160         int     padding[56];
161 };
162
163 /* signal.h */
164 #define IA32_SET_SA_HANDLER(ka,handler,restorer)                                \
165                                 ((ka)->sa.sa_handler = (__sighandler_t)         \
166                                         (((unsigned long)(restorer) << 32)      \
167                                          | ((handler) & 0xffffffff)))
168 #define IA32_SA_HANDLER(ka)     ((unsigned long) (ka)->sa.sa_handler & 0xffffffff)
169 #define IA32_SA_RESTORER(ka)    ((unsigned long) (ka)->sa.sa_handler >> 32)
170
171 #define __IA32_NR_sigreturn 119
172 #define __IA32_NR_rt_sigreturn 173
173
174 struct sigaction32 {
175        unsigned int sa_handler;         /* Really a pointer, but need to deal with 32 bits */
176        unsigned int sa_flags;
177        unsigned int sa_restorer;        /* Another 32 bit pointer */
178        compat_sigset_t sa_mask;         /* A 32 bit mask */
179 };
180
181 struct old_sigaction32 {
182        unsigned int  sa_handler;        /* Really a pointer, but need to deal
183                                              with 32 bits */
184        compat_old_sigset_t sa_mask;             /* A 32 bit mask */
185        unsigned int sa_flags;
186        unsigned int sa_restorer;        /* Another 32 bit pointer */
187 };
188
189 typedef struct sigaltstack_ia32 {
190         unsigned int    ss_sp;
191         int             ss_flags;
192         unsigned int    ss_size;
193 } stack_ia32_t;
194
195 struct ucontext_ia32 {
196         unsigned int      uc_flags;
197         unsigned int      uc_link;
198         stack_ia32_t      uc_stack;
199         struct sigcontext_ia32 uc_mcontext;
200         sigset_t          uc_sigmask;   /* mask last for extensibility */
201 };
202
203 struct stat64 {
204         unsigned long long      st_dev;
205         unsigned char   __pad0[4];
206         unsigned int    __st_ino;
207         unsigned int    st_mode;
208         unsigned int    st_nlink;
209         unsigned int    st_uid;
210         unsigned int    st_gid;
211         unsigned long long      st_rdev;
212         unsigned char   __pad3[4];
213         unsigned int    st_size_lo;
214         unsigned int    st_size_hi;
215         unsigned int    st_blksize;
216         unsigned int    st_blocks;      /* Number 512-byte blocks allocated. */
217         unsigned int    __pad4;         /* future possible st_blocks high bits */
218         unsigned int    st_atime;
219         unsigned int    st_atime_nsec;
220         unsigned int    st_mtime;
221         unsigned int    st_mtime_nsec;
222         unsigned int    st_ctime;
223         unsigned int    st_ctime_nsec;
224         unsigned int    st_ino_lo;
225         unsigned int    st_ino_hi;
226 };
227
228 typedef union sigval32 {
229         int sival_int;
230         unsigned int sival_ptr;
231 } sigval_t32;
232
233 #define SIGEV_PAD_SIZE32 ((SIGEV_MAX_SIZE/sizeof(int)) - 3)
234
235 typedef struct compat_siginfo {
236         int si_signo;
237         int si_errno;
238         int si_code;
239
240         union {
241                 int _pad[((128/sizeof(int)) - 3)];
242
243                 /* kill() */
244                 struct {
245                         unsigned int _pid;      /* sender's pid */
246                         unsigned int _uid;      /* sender's uid */
247                 } _kill;
248
249                 /* POSIX.1b timers */
250                 struct {
251                         timer_t _tid;           /* timer id */
252                         int _overrun;           /* overrun count */
253                         char _pad[sizeof(unsigned int) - sizeof(int)];
254                         sigval_t32 _sigval;     /* same as below */
255                         int _sys_private;       /* not to be passed to user */
256                 } _timer;
257
258                 /* POSIX.1b signals */
259                 struct {
260                         unsigned int _pid;      /* sender's pid */
261                         unsigned int _uid;      /* sender's uid */
262                         sigval_t32 _sigval;
263                 } _rt;
264
265                 /* SIGCHLD */
266                 struct {
267                         unsigned int _pid;      /* which child */
268                         unsigned int _uid;      /* sender's uid */
269                         int _status;            /* exit code */
270                         compat_clock_t _utime;
271                         compat_clock_t _stime;
272                 } _sigchld;
273
274                 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
275                 struct {
276                         unsigned int _addr;     /* faulting insn/memory ref. */
277                 } _sigfault;
278
279                 /* SIGPOLL */
280                 struct {
281                         int _band;      /* POLL_IN, POLL_OUT, POLL_MSG */
282                         int _fd;
283                 } _sigpoll;
284         } _sifields;
285 } compat_siginfo_t;
286
287 typedef struct sigevent32 {
288         sigval_t32 sigev_value;
289         int sigev_signo;
290         int sigev_notify;
291         union {
292                 int _pad[SIGEV_PAD_SIZE32];
293                 struct {
294                         u32 _function;
295                         u32 _attribute; /* really pthread_attr_t */
296                 } _sigev_thread;
297         } _sigev_un;
298 } sigevent_t32;
299
300 struct old_linux32_dirent {
301         u32     d_ino;
302         u32     d_offset;
303         u16     d_namlen;
304         char    d_name[1];
305 };
306
307 /*
308  * IA-32 ELF specific definitions for IA-64.
309  */
310
311 #define _ASM_IA64_ELF_H         /* Don't include elf.h */
312
313 #include <linux/sched.h>
314 #include <asm/processor.h>
315
316 /*
317  * This is used to ensure we don't load something for the wrong architecture.
318  */
319 #define elf_check_arch(x) ((x)->e_machine == EM_386)
320
321 /*
322  * These are used to set parameters in the core dumps.
323  */
324 #define ELF_CLASS       ELFCLASS32
325 #define ELF_DATA        ELFDATA2LSB
326 #define ELF_ARCH        EM_386
327
328 #define IA32_PAGE_OFFSET        0xc0000000
329 #define IA32_STACK_TOP          IA32_PAGE_OFFSET
330 #define IA32_GATE_OFFSET        IA32_PAGE_OFFSET
331 #define IA32_GATE_END           IA32_PAGE_OFFSET + PAGE_SIZE
332
333 /*
334  * The system segments (GDT, TSS, LDT) have to be mapped below 4GB so the IA-32 engine can
335  * access them.
336  */
337 #define IA32_GDT_OFFSET         (IA32_PAGE_OFFSET + PAGE_SIZE)
338 #define IA32_TSS_OFFSET         (IA32_PAGE_OFFSET + 2*PAGE_SIZE)
339 #define IA32_LDT_OFFSET         (IA32_PAGE_OFFSET + 3*PAGE_SIZE)
340
341 #define ELF_EXEC_PAGESIZE       IA32_PAGE_SIZE
342
343 /*
344  * This is the location that an ET_DYN program is loaded if exec'ed.
345  * Typical use of this is to invoke "./ld.so someprog" to test out a
346  * new version of the loader.  We need to make sure that it is out of
347  * the way of the program that it will "exec", and that there is
348  * sufficient room for the brk.
349  */
350 #define ELF_ET_DYN_BASE         (IA32_PAGE_OFFSET/3 + 0x1000000)
351
352 void ia64_elf32_init(struct pt_regs *regs);
353 #define ELF_PLAT_INIT(_r, load_addr)    ia64_elf32_init(_r)
354
355 #define elf_addr_t      u32
356
357 /* This macro yields a bitmask that programs can use to figure out
358    what instruction set this CPU supports.  */
359 #define ELF_HWCAP       0
360
361 /* This macro yields a string that ld.so will use to load
362    implementation specific libraries for optimization.  Not terribly
363    relevant until we have real hardware to play with... */
364 #define ELF_PLATFORM    NULL
365
366 #ifdef __KERNEL__
367 # define SET_PERSONALITY(EX,IBCS2)                              \
368         (current->personality = (IBCS2) ? PER_SVR4 : PER_LINUX)
369 #endif
370
371 #define IA32_EFLAG      0x200
372
373 /*
374  * IA-32 ELF specific definitions for IA-64.
375  */
376
377 #define __USER_CS      0x23
378 #define __USER_DS      0x2B
379
380 /*
381  * The per-cpu GDT has 32 entries: see <asm-i386/segment.h>
382  */
383 #define GDT_ENTRIES 32
384
385 #define GDT_SIZE        (GDT_ENTRIES * 8)
386
387 #define TSS_ENTRY 14
388 #define LDT_ENTRY       (TSS_ENTRY + 1)
389
390 #define IA32_SEGSEL_RPL         (0x3 << 0)
391 #define IA32_SEGSEL_TI          (0x1 << 2)
392 #define IA32_SEGSEL_INDEX_SHIFT 3
393
394 #define _TSS                    ((unsigned long) TSS_ENTRY << IA32_SEGSEL_INDEX_SHIFT)
395 #define _LDT                    ((unsigned long) LDT_ENTRY << IA32_SEGSEL_INDEX_SHIFT)
396
397 #define IA32_SEG_BASE           16
398 #define IA32_SEG_TYPE           40
399 #define IA32_SEG_SYS            44
400 #define IA32_SEG_DPL            45
401 #define IA32_SEG_P              47
402 #define IA32_SEG_HIGH_LIMIT     48
403 #define IA32_SEG_AVL            52
404 #define IA32_SEG_DB             54
405 #define IA32_SEG_G              55
406 #define IA32_SEG_HIGH_BASE      56
407
408 #define IA32_SEG_DESCRIPTOR(base, limit, segtype, nonsysseg, dpl, segpresent, avl, segdb, gran) \
409                (((limit) & 0xffff)                                                              \
410                 | (((unsigned long) (base) & 0xffffff) << IA32_SEG_BASE)                        \
411                 | ((unsigned long) (segtype) << IA32_SEG_TYPE)                                  \
412                 | ((unsigned long) (nonsysseg) << IA32_SEG_SYS)                                 \
413                 | ((unsigned long) (dpl) << IA32_SEG_DPL)                                       \
414                 | ((unsigned long) (segpresent) << IA32_SEG_P)                                  \
415                 | ((((unsigned long) (limit) >> 16) & 0xf) << IA32_SEG_HIGH_LIMIT)              \
416                 | ((unsigned long) (avl) << IA32_SEG_AVL)                                       \
417                 | ((unsigned long) (segdb) << IA32_SEG_DB)                                      \
418                 | ((unsigned long) (gran) << IA32_SEG_G)                                        \
419                 | ((((unsigned long) (base) >> 24) & 0xff) << IA32_SEG_HIGH_BASE))
420
421 #define SEG_LIM         32
422 #define SEG_TYPE        52
423 #define SEG_SYS         56
424 #define SEG_DPL         57
425 #define SEG_P           59
426 #define SEG_AVL         60
427 #define SEG_DB          62
428 #define SEG_G           63
429
430 /* Unscramble an IA-32 segment descriptor into the IA-64 format.  */
431 #define IA32_SEG_UNSCRAMBLE(sd)                                                                  \
432         (   (((sd) >> IA32_SEG_BASE) & 0xffffff) | ((((sd) >> IA32_SEG_HIGH_BASE) & 0xff) << 24) \
433          | ((((sd) & 0xffff) | ((((sd) >> IA32_SEG_HIGH_LIMIT) & 0xf) << 16)) << SEG_LIM)        \
434          | ((((sd) >> IA32_SEG_TYPE) & 0xf) << SEG_TYPE)                                         \
435          | ((((sd) >> IA32_SEG_SYS) & 0x1) << SEG_SYS)                                           \
436          | ((((sd) >> IA32_SEG_DPL) & 0x3) << SEG_DPL)                                           \
437          | ((((sd) >> IA32_SEG_P) & 0x1) << SEG_P)                                               \
438          | ((((sd) >> IA32_SEG_AVL) & 0x1) << SEG_AVL)                                           \
439          | ((((sd) >> IA32_SEG_DB) & 0x1) << SEG_DB)                                             \
440          | ((((sd) >> IA32_SEG_G) & 0x1) << SEG_G))
441
442 #define IA32_IOBASE     0x2000000000000000UL /* Virtual address for I/O space */
443
444 #define IA32_CR0        0x80000001      /* Enable PG and PE bits */
445 #define IA32_CR4        0x600           /* MMXEX and FXSR on */
446
447 /*
448  *  IA32 floating point control registers starting values
449  */
450
451 #define IA32_FSR_DEFAULT        0x55550000              /* set all tag bits */
452 #define IA32_FCR_DEFAULT        0x17800000037fUL        /* extended precision, all masks */
453
454 #define IA32_PTRACE_GETREGS     12
455 #define IA32_PTRACE_SETREGS     13
456 #define IA32_PTRACE_GETFPREGS   14
457 #define IA32_PTRACE_SETFPREGS   15
458 #define IA32_PTRACE_GETFPXREGS  18
459 #define IA32_PTRACE_SETFPXREGS  19
460
461 #define ia32_start_thread(regs,new_ip,new_sp) do {                              \
462         set_fs(USER_DS);                                                        \
463         ia64_psr(regs)->cpl = 3;        /* set user mode */                     \
464         ia64_psr(regs)->ri = 0;         /* clear return slot number */          \
465         ia64_psr(regs)->is = 1;         /* IA-32 instruction set */             \
466         regs->cr_iip = new_ip;                                                  \
467         regs->ar_rsc = 0xc;             /* enforced lazy mode, priv. level 3 */ \
468         regs->ar_rnat = 0;                                                      \
469         regs->loadrs = 0;                                                       \
470         regs->r12 = new_sp;                                                     \
471 } while (0)
472
473 /*
474  * Local Descriptor Table (LDT) related declarations.
475  */
476
477 #define IA32_LDT_ENTRIES        8192            /* Maximum number of LDT entries supported. */
478 #define IA32_LDT_ENTRY_SIZE     8               /* The size of each LDT entry. */
479
480 #define LDT_entry_a(info) \
481         ((((info)->base_addr & 0x0000ffff) << 16) | ((info)->limit & 0x0ffff))
482
483 #define LDT_entry_b(info)                               \
484         (((info)->base_addr & 0xff000000) |             \
485         (((info)->base_addr & 0x00ff0000) >> 16) |      \
486         ((info)->limit & 0xf0000) |                     \
487         (((info)->read_exec_only ^ 1) << 9) |           \
488         ((info)->contents << 10) |                      \
489         (((info)->seg_not_present ^ 1) << 15) |         \
490         ((info)->seg_32bit << 22) |                     \
491         ((info)->limit_in_pages << 23) |                \
492         ((info)->useable << 20) |                       \
493         0x7100)
494
495 #define LDT_empty(info) (                       \
496         (info)->base_addr       == 0    &&      \
497         (info)->limit           == 0    &&      \
498         (info)->contents        == 0    &&      \
499         (info)->read_exec_only  == 1    &&      \
500         (info)->seg_32bit       == 0    &&      \
501         (info)->limit_in_pages  == 0    &&      \
502         (info)->seg_not_present == 1    &&      \
503         (info)->useable         == 0    )
504
505 static inline void
506 load_TLS (struct thread_struct *t, unsigned int cpu)
507 {
508         extern unsigned long *cpu_gdt_table[NR_CPUS];
509
510         memcpy(cpu_gdt_table[cpu] + GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0], sizeof(long));
511         memcpy(cpu_gdt_table[cpu] + GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1], sizeof(long));
512         memcpy(cpu_gdt_table[cpu] + GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2], sizeof(long));
513 }
514
515 struct ia32_user_desc {
516         unsigned int entry_number;
517         unsigned int base_addr;
518         unsigned int limit;
519         unsigned int seg_32bit:1;
520         unsigned int contents:2;
521         unsigned int read_exec_only:1;
522         unsigned int limit_in_pages:1;
523         unsigned int seg_not_present:1;
524         unsigned int useable:1;
525 };
526
527 struct linux_binprm;
528
529 extern void ia32_init_addr_space (struct pt_regs *regs);
530 extern int ia32_setup_arg_pages (struct linux_binprm *bprm, int exec_stack);
531 extern unsigned long ia32_do_mmap (struct file *, unsigned long, unsigned long, int, int, loff_t);
532 extern void ia32_load_segment_descriptors (struct task_struct *task);
533
534 #define ia32f2ia64f(dst,src)                    \
535 do {                                            \
536         ia64_ldfe(6,src);                       \
537         ia64_stop();                            \
538         ia64_stf_spill(dst, 6);                 \
539 } while(0)
540
541 #define ia64f2ia32f(dst,src)                    \
542 do {                                            \
543         ia64_ldf_fill(6, src);                  \
544         ia64_stop();                            \
545         ia64_stfe(dst, 6);                      \
546 } while(0)
547
548 struct user_regs_struct32 {
549         __u32 ebx, ecx, edx, esi, edi, ebp, eax;
550         unsigned short ds, __ds, es, __es;
551         unsigned short fs, __fs, gs, __gs;
552         __u32 orig_eax, eip;
553         unsigned short cs, __cs;
554         __u32 eflags, esp;
555         unsigned short ss, __ss;
556 };
557
558 /* Prototypes for use in elfcore32.h */
559 extern int save_ia32_fpstate (struct task_struct *, struct ia32_user_i387_struct __user *);
560 extern int save_ia32_fpxstate (struct task_struct *, struct ia32_user_fxsr_struct __user *);
561
562 #endif /* !CONFIG_IA32_SUPPORT */
563
564 #endif /* _ASM_IA64_IA32_PRIV_H */