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