1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2004 Tim J. Robbins 5 * Copyright (c) 2001 Doug Rabson 6 * Copyright (c) 1994-1996 Søren Schmidt 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer 14 * in this position and unchanged. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 #ifndef _AMD64_LINUX_H_ 36 #define _AMD64_LINUX_H_ 37 38 #include <sys/abi_compat.h> 39 40 #include <compat/linux/linux.h> 41 #include <amd64/linux32/linux32_syscall.h> 42 43 #define LINUX_LEGACY_SYSCALLS 44 45 #define LINUX_DTRACE linuxulator32 46 47 #define LINUX32_MAXDSIZ (512 * 1024 * 1024) /* 512MB */ 48 #define LINUX32_MAXSSIZ (64 * 1024 * 1024) /* 64MB */ 49 #define LINUX32_MAXVMEM 0 /* Unlimited */ 50 51 /* 52 * Provide a separate set of types for the Linux types. 53 */ 54 typedef int l_int; 55 typedef int32_t l_long; 56 typedef int64_t l_longlong; 57 typedef short l_short; 58 typedef unsigned int l_uint; 59 typedef uint32_t l_ulong; 60 typedef uint64_t l_ulonglong; 61 typedef unsigned short l_ushort; 62 63 typedef l_ulong l_uintptr_t; 64 typedef l_long l_clock_t; 65 typedef l_int l_daddr_t; 66 typedef l_ushort l_dev_t; 67 typedef l_uint l_gid_t; 68 typedef l_ushort l_gid16_t; 69 typedef l_ulong l_ino_t; 70 typedef l_int l_key_t; 71 typedef l_longlong l_loff_t; 72 typedef l_ushort l_mode_t; 73 typedef l_long l_off_t; 74 typedef l_int l_pid_t; 75 typedef l_uint l_size_t; 76 typedef l_long l_suseconds_t; 77 typedef l_long l_time_t; 78 typedef l_longlong l_time64_t; 79 typedef l_uint l_uid_t; 80 typedef l_ushort l_uid16_t; 81 typedef l_int l_timer_t; 82 typedef l_int l_mqd_t; 83 typedef l_ulong l_fd_mask; 84 85 typedef struct { 86 l_int val[2]; 87 } l_fsid_t; 88 89 typedef struct { 90 l_time_t tv_sec; 91 l_suseconds_t tv_usec; 92 } l_timeval; 93 94 #define l_fd_set fd_set 95 96 /* 97 * Miscellaneous 98 */ 99 #define LINUX_AT_COUNT 21 /* Count of used aux entry types. 100 * Keep this synchronized with 101 * linux_fixup_elf() code. 102 */ 103 struct l___sysctl_args 104 { 105 l_uintptr_t name; 106 l_int nlen; 107 l_uintptr_t oldval; 108 l_uintptr_t oldlenp; 109 l_uintptr_t newval; 110 l_size_t newlen; 111 l_ulong __spare[4]; 112 }; 113 114 /* Resource limits */ 115 #define LINUX_RLIMIT_CPU 0 116 #define LINUX_RLIMIT_FSIZE 1 117 #define LINUX_RLIMIT_DATA 2 118 #define LINUX_RLIMIT_STACK 3 119 #define LINUX_RLIMIT_CORE 4 120 #define LINUX_RLIMIT_RSS 5 121 #define LINUX_RLIMIT_NPROC 6 122 #define LINUX_RLIMIT_NOFILE 7 123 #define LINUX_RLIMIT_MEMLOCK 8 124 #define LINUX_RLIMIT_AS 9 /* Address space limit */ 125 126 #define LINUX_RLIM_NLIMITS 10 127 128 struct l_rlimit { 129 l_ulong rlim_cur; 130 l_ulong rlim_max; 131 }; 132 133 struct l_rusage { 134 l_timeval ru_utime; 135 l_timeval ru_stime; 136 l_long ru_maxrss; 137 l_long ru_ixrss; 138 l_long ru_idrss; 139 l_long ru_isrss; 140 l_long ru_minflt; 141 l_long ru_majflt; 142 l_long ru_nswap; 143 l_long ru_inblock; 144 l_long ru_oublock; 145 l_long ru_msgsnd; 146 l_long ru_msgrcv; 147 l_long ru_nsignals; 148 l_long ru_nvcsw; 149 l_long ru_nivcsw; 150 }; 151 152 struct l_mmap_argv { 153 l_uintptr_t addr; 154 l_size_t len; 155 l_int prot; 156 l_int flags; 157 l_int fd; 158 l_ulong pgoff; 159 }; 160 161 /* 162 * stat family of syscalls 163 */ 164 struct l_timespec { 165 l_time_t tv_sec; 166 l_long tv_nsec; 167 }; 168 169 /* __kernel_timespec */ 170 struct l_timespec64 { 171 l_time64_t tv_sec; 172 l_longlong tv_nsec; 173 }; 174 175 struct l_newstat { 176 l_ushort st_dev; 177 l_ushort __pad1; 178 l_ulong st_ino; 179 l_ushort st_mode; 180 l_ushort st_nlink; 181 l_ushort st_uid; 182 l_ushort st_gid; 183 l_ushort st_rdev; 184 l_ushort __pad2; 185 l_ulong st_size; 186 l_ulong st_blksize; 187 l_ulong st_blocks; 188 struct l_timespec st_atim; 189 struct l_timespec st_mtim; 190 struct l_timespec st_ctim; 191 l_ulong __unused4; 192 l_ulong __unused5; 193 }; 194 195 struct l_stat { 196 l_ushort st_dev; 197 l_ulong st_ino; 198 l_ushort st_mode; 199 l_ushort st_nlink; 200 l_ushort st_uid; 201 l_ushort st_gid; 202 l_ushort st_rdev; 203 l_long st_size; 204 struct l_timespec st_atim; 205 struct l_timespec st_mtim; 206 struct l_timespec st_ctim; 207 l_long st_blksize; 208 l_long st_blocks; 209 l_ulong st_flags; 210 l_ulong st_gen; 211 }; 212 213 struct l_stat64 { 214 l_ushort st_dev; 215 u_char __pad0[10]; 216 l_ulong __st_ino; 217 l_uint st_mode; 218 l_uint st_nlink; 219 l_ulong st_uid; 220 l_ulong st_gid; 221 l_ushort st_rdev; 222 u_char __pad3[10]; 223 l_longlong st_size; 224 l_ulong st_blksize; 225 l_ulong st_blocks; 226 l_ulong __pad4; 227 struct l_timespec st_atim; 228 struct l_timespec st_mtim; 229 struct l_timespec st_ctim; 230 l_ulonglong st_ino; 231 } __packed; 232 233 struct l_statfs64 { 234 l_int f_type; 235 l_int f_bsize; 236 uint64_t f_blocks; 237 uint64_t f_bfree; 238 uint64_t f_bavail; 239 uint64_t f_files; 240 uint64_t f_ffree; 241 l_fsid_t f_fsid; 242 l_int f_namelen; 243 l_int f_frsize; 244 l_int f_flags; 245 l_int f_spare[4]; 246 } __packed; 247 248 /* sigaction flags */ 249 #define LINUX_SA_NOCLDSTOP 0x00000001 250 #define LINUX_SA_NOCLDWAIT 0x00000002 251 #define LINUX_SA_SIGINFO 0x00000004 252 #define LINUX_SA_RESTORER 0x04000000 253 #define LINUX_SA_ONSTACK 0x08000000 254 #define LINUX_SA_RESTART 0x10000000 255 #define LINUX_SA_INTERRUPT 0x20000000 256 #define LINUX_SA_NOMASK 0x40000000 257 #define LINUX_SA_ONESHOT 0x80000000 258 259 /* sigprocmask actions */ 260 #define LINUX_SIG_BLOCK 0 261 #define LINUX_SIG_UNBLOCK 1 262 #define LINUX_SIG_SETMASK 2 263 264 /* sigaltstack */ 265 #define LINUX_MINSIGSTKSZ 2048 266 267 typedef l_uintptr_t l_handler_t; 268 typedef l_ulong l_osigset_t; 269 270 typedef struct { 271 l_handler_t lsa_handler; 272 l_osigset_t lsa_mask; 273 l_ulong lsa_flags; 274 l_uintptr_t lsa_restorer; 275 } l_osigaction_t; 276 277 typedef struct { 278 l_handler_t lsa_handler; 279 l_ulong lsa_flags; 280 l_uintptr_t lsa_restorer; 281 l_sigset_t lsa_mask; 282 } __packed l_sigaction_t; 283 284 typedef struct { 285 l_uintptr_t ss_sp; 286 l_int ss_flags; 287 l_size_t ss_size; 288 } l_stack_t; 289 290 /* The Linux sigcontext, pretty much a standard 386 trapframe. */ 291 struct l_sigcontext { 292 l_uint sc_gs; 293 l_uint sc_fs; 294 l_uint sc_es; 295 l_uint sc_ds; 296 l_uint sc_edi; 297 l_uint sc_esi; 298 l_uint sc_ebp; 299 l_uint sc_esp; 300 l_uint sc_ebx; 301 l_uint sc_edx; 302 l_uint sc_ecx; 303 l_uint sc_eax; 304 l_uint sc_trapno; 305 l_uint sc_err; 306 l_uint sc_eip; 307 l_uint sc_cs; 308 l_uint sc_eflags; 309 l_uint sc_esp_at_signal; 310 l_uint sc_ss; 311 l_uint sc_387; 312 l_uint sc_mask; 313 l_uint sc_cr2; 314 }; 315 316 struct l_ucontext { 317 l_ulong uc_flags; 318 l_uintptr_t uc_link; 319 l_stack_t uc_stack; 320 struct l_sigcontext uc_mcontext; 321 l_sigset_t uc_sigmask; 322 } __packed; 323 324 #define LINUX_SI_MAX_SIZE 128 325 #define LINUX_SI_PAD_SIZE ((LINUX_SI_MAX_SIZE/sizeof(l_int)) - 3) 326 327 typedef union l_sigval { 328 l_int sival_int; 329 l_uintptr_t sival_ptr; 330 } l_sigval_t; 331 332 typedef struct l_siginfo { 333 l_int lsi_signo; 334 l_int lsi_errno; 335 l_int lsi_code; 336 union { 337 l_int _pad[LINUX_SI_PAD_SIZE]; 338 339 struct { 340 l_pid_t _pid; 341 l_uid_t _uid; 342 } _kill; 343 344 struct { 345 l_timer_t _tid; 346 l_int _overrun; 347 char _pad[sizeof(l_uid_t) - sizeof(l_int)]; 348 l_sigval_t _sigval; 349 l_int _sys_private; 350 } _timer; 351 352 struct { 353 l_pid_t _pid; /* sender's pid */ 354 l_uid_t _uid; /* sender's uid */ 355 l_sigval_t _sigval; 356 } _rt; 357 358 struct { 359 l_pid_t _pid; /* which child */ 360 l_uid_t _uid; /* sender's uid */ 361 l_int _status; /* exit code */ 362 l_clock_t _utime; 363 l_clock_t _stime; 364 } _sigchld; 365 366 struct { 367 l_uintptr_t _addr; /* Faulting insn/memory ref. */ 368 } _sigfault; 369 370 struct { 371 l_long _band; /* POLL_IN,POLL_OUT,POLL_MSG */ 372 l_int _fd; 373 } _sigpoll; 374 } _sifields; 375 } l_siginfo_t; 376 377 #define lsi_pid _sifields._kill._pid 378 #define lsi_uid _sifields._kill._uid 379 #define lsi_tid _sifields._timer._tid 380 #define lsi_overrun _sifields._timer._overrun 381 #define lsi_sys_private _sifields._timer._sys_private 382 #define lsi_status _sifields._sigchld._status 383 #define lsi_utime _sifields._sigchld._utime 384 #define lsi_stime _sifields._sigchld._stime 385 #define lsi_value _sifields._rt._sigval 386 #define lsi_int _sifields._rt._sigval.sival_int 387 #define lsi_ptr _sifields._rt._sigval.sival_ptr 388 #define lsi_addr _sifields._sigfault._addr 389 #define lsi_band _sifields._sigpoll._band 390 #define lsi_fd _sifields._sigpoll._fd 391 392 struct l_fpreg { 393 u_int16_t significand[4]; 394 u_int16_t exponent; 395 }; 396 397 struct l_fpxreg { 398 u_int16_t significand[4]; 399 u_int16_t exponent; 400 u_int16_t padding[3]; 401 }; 402 403 struct l_xmmreg { 404 u_int32_t element[4]; 405 }; 406 407 struct l_fpstate { 408 /* Regular FPU environment */ 409 u_int32_t cw; 410 u_int32_t sw; 411 u_int32_t tag; 412 u_int32_t ipoff; 413 u_int32_t cssel; 414 u_int32_t dataoff; 415 u_int32_t datasel; 416 struct l_fpreg _st[8]; 417 u_int16_t status; 418 u_int16_t magic; /* 0xffff = regular FPU data */ 419 420 /* FXSR FPU environment */ 421 u_int32_t _fxsr_env[6]; /* env is ignored. */ 422 u_int32_t mxcsr; 423 u_int32_t reserved; 424 struct l_fpxreg _fxsr_st[8]; /* reg data is ignored. */ 425 struct l_xmmreg _xmm[8]; 426 u_int32_t padding[56]; 427 }; 428 429 /* 430 * We make the stack look like Linux expects it when calling a signal 431 * handler, but use the BSD way of calling the handler and sigreturn(). 432 */ 433 struct l_sigframe { 434 l_int sf_sig; 435 struct l_sigcontext sf_sc; 436 struct l_fpstate sf_fpstate; 437 l_uint sf_extramask[1]; 438 }; 439 440 struct l_rt_sigframe { 441 l_int sf_sig; 442 l_uintptr_t sf_siginfo; 443 l_uintptr_t sf_ucontext; 444 l_siginfo_t sf_si; 445 struct l_ucontext sf_sc; 446 } __packed; 447 448 /* 449 * arch specific open/fcntl flags 450 */ 451 #define LINUX_F_GETLK64 12 452 #define LINUX_F_SETLK64 13 453 #define LINUX_F_SETLKW64 14 454 455 union l_semun { 456 l_int val; 457 l_uintptr_t buf; 458 l_uintptr_t array; 459 l_uintptr_t __buf; 460 l_uintptr_t __pad; 461 }; 462 463 struct l_ifmap { 464 l_ulong mem_start; 465 l_ulong mem_end; 466 l_ushort base_addr; 467 u_char irq; 468 u_char dma; 469 u_char port; 470 /* 3 bytes spare */ 471 }; 472 473 struct l_ifreq { 474 union { 475 char ifrn_name[LINUX_IFNAMSIZ]; 476 } ifr_ifrn; 477 478 union { 479 struct l_sockaddr ifru_addr; 480 struct l_sockaddr ifru_dstaddr; 481 struct l_sockaddr ifru_broadaddr; 482 struct l_sockaddr ifru_netmask; 483 struct l_sockaddr ifru_hwaddr; 484 l_short ifru_flags[1]; 485 l_int ifru_ivalue; 486 l_int ifru_mtu; 487 struct l_ifmap ifru_map; 488 char ifru_slave[LINUX_IFNAMSIZ]; 489 l_uintptr_t ifru_data; 490 } ifr_ifru; 491 }; 492 493 #define ifr_name ifr_ifrn.ifrn_name /* Interface name */ 494 #define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */ 495 #define ifr_ifindex ifr_ifru.ifru_ivalue /* Interface index */ 496 497 struct l_ifconf { 498 int ifc_len; 499 union { 500 l_uintptr_t ifcu_buf; 501 l_uintptr_t ifcu_req; 502 } ifc_ifcu; 503 }; 504 505 #define ifc_buf ifc_ifcu.ifcu_buf 506 #define ifc_req ifc_ifcu.ifcu_req 507 508 struct l_user_desc { 509 l_uint entry_number; 510 l_uint base_addr; 511 l_uint limit; 512 l_uint seg_32bit:1; 513 l_uint contents:2; 514 l_uint read_exec_only:1; 515 l_uint limit_in_pages:1; 516 l_uint seg_not_present:1; 517 l_uint useable:1; 518 }; 519 520 #define LINUX_LOWERWORD 0x0000ffff 521 522 /* 523 * Macros which does the same thing as those in Linux include/asm-um/ldt-i386.h. 524 * These convert Linux user space descriptor to machine one. 525 */ 526 #define LINUX_LDT_entry_a(info) \ 527 ((((info)->base_addr & LINUX_LOWERWORD) << 16) | \ 528 ((info)->limit & LINUX_LOWERWORD)) 529 530 #define LINUX_ENTRY_B_READ_EXEC_ONLY 9 531 #define LINUX_ENTRY_B_CONTENTS 10 532 #define LINUX_ENTRY_B_SEG_NOT_PRESENT 15 533 #define LINUX_ENTRY_B_BASE_ADDR 16 534 #define LINUX_ENTRY_B_USEABLE 20 535 #define LINUX_ENTRY_B_SEG32BIT 22 536 #define LINUX_ENTRY_B_LIMIT 23 537 538 #define LINUX_LDT_entry_b(info) \ 539 (((info)->base_addr & 0xff000000) | \ 540 ((info)->limit & 0xf0000) | \ 541 ((info)->contents << LINUX_ENTRY_B_CONTENTS) | \ 542 (((info)->seg_not_present == 0) << LINUX_ENTRY_B_SEG_NOT_PRESENT) | \ 543 (((info)->base_addr & 0x00ff0000) >> LINUX_ENTRY_B_BASE_ADDR) | \ 544 (((info)->read_exec_only == 0) << LINUX_ENTRY_B_READ_EXEC_ONLY) | \ 545 ((info)->seg_32bit << LINUX_ENTRY_B_SEG32BIT) | \ 546 ((info)->useable << LINUX_ENTRY_B_USEABLE) | \ 547 ((info)->limit_in_pages << LINUX_ENTRY_B_LIMIT) | 0x7000) 548 549 #define LINUX_LDT_empty(info) \ 550 ((info)->base_addr == 0 && \ 551 (info)->limit == 0 && \ 552 (info)->contents == 0 && \ 553 (info)->seg_not_present == 1 && \ 554 (info)->read_exec_only == 1 && \ 555 (info)->seg_32bit == 0 && \ 556 (info)->limit_in_pages == 0 && \ 557 (info)->useable == 0) 558 559 /* 560 * Macros for converting segments. 561 * They do the same as those in arch/i386/kernel/process.c in Linux. 562 */ 563 #define LINUX_GET_BASE(desc) \ 564 ((((desc)->a >> 16) & LINUX_LOWERWORD) | \ 565 (((desc)->b << 16) & 0x00ff0000) | \ 566 ((desc)->b & 0xff000000)) 567 568 #define LINUX_GET_LIMIT(desc) \ 569 (((desc)->a & LINUX_LOWERWORD) | \ 570 ((desc)->b & 0xf0000)) 571 572 #define LINUX_GET_32BIT(desc) \ 573 (((desc)->b >> LINUX_ENTRY_B_SEG32BIT) & 1) 574 #define LINUX_GET_CONTENTS(desc) \ 575 (((desc)->b >> LINUX_ENTRY_B_CONTENTS) & 3) 576 #define LINUX_GET_WRITABLE(desc) \ 577 (((desc)->b >> LINUX_ENTRY_B_READ_EXEC_ONLY) & 1) 578 #define LINUX_GET_LIMIT_PAGES(desc) \ 579 (((desc)->b >> LINUX_ENTRY_B_LIMIT) & 1) 580 #define LINUX_GET_PRESENT(desc) \ 581 (((desc)->b >> LINUX_ENTRY_B_SEG_NOT_PRESENT) & 1) 582 #define LINUX_GET_USEABLE(desc) \ 583 (((desc)->b >> LINUX_ENTRY_B_USEABLE) & 1) 584 585 struct iovec; 586 struct uio; 587 588 struct l_iovec32 { 589 uint32_t iov_base; 590 l_size_t iov_len; 591 }; 592 593 int linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, 594 struct iovec **iovp, int error); 595 int linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, 596 struct uio **uiop); 597 int linux_copyout_rusage(struct rusage *ru, void *uaddr); 598 599 /* robust futexes */ 600 struct linux_robust_list { 601 l_uintptr_t next; 602 }; 603 604 struct linux_robust_list_head { 605 struct linux_robust_list list; 606 l_long futex_offset; 607 l_uintptr_t pending_list; 608 }; 609 610 /* This corresponds to 'struct user_regs_struct32' in Linux. */ 611 struct linux_pt_regset32 { 612 l_uint ebx; 613 l_uint ecx; 614 l_uint edx; 615 l_uint esi; 616 l_uint edi; 617 l_uint ebp; 618 l_uint eax; 619 l_uint ds; 620 l_uint es; 621 l_uint fs; 622 l_uint gs; 623 l_uint orig_eax; 624 l_uint eip; 625 l_uint cs; 626 l_uint eflags; 627 l_uint esp; 628 l_uint ss; 629 }; 630 631 struct reg32; 632 633 void bsd_to_linux_regset32(const struct reg32 *b_reg, 634 struct linux_pt_regset32 *l_regset); 635 636 extern bool linux32_emulate_i386; 637 638 #endif /* !_AMD64_LINUX_H_ */ 639