From c200fec2fccf2f11bd358bf8faaebc255a6af232 Mon Sep 17 00:00:00 2001 From: Daniel McKenna Date: Mon, 22 May 2017 19:33:40 +0100 Subject: [PATCH 1/5] Added structs for user register values. Added structs defined in sys/user.h adapted to follow rust naming conventions (so user_regs_struct becomes user::Regs). --- src/sys/mod.rs | 3 + src/sys/user.rs | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 src/sys/user.rs diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 7675f94448..a7bd2a2441 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -59,6 +59,9 @@ pub mod time; )] pub mod ptrace; +#[cfg(all(target_os = "linux"))] +pub mod user; + pub mod select; #[cfg(all(target_os = "linux", diff --git a/src/sys/user.rs b/src/sys/user.rs new file mode 100644 index 0000000000..6f027311bc --- /dev/null +++ b/src/sys/user.rs @@ -0,0 +1,154 @@ + + +#[cfg(any(target_arch = "x86_64"))] +use libc::{c_ushort, c_uint, c_char, c_int, c_longlong, c_ulonglong}; +#[cfg(any(target_arch = "x86"))] +use libc::{c_ushort, c_uint, c_char, c_int, c_long, c_ulong}; + + +#[repr(C)] +#[cfg(any(target_arch = "x86_64"))] +pub struct FpRegs { + pub cwd: c_ushort, + pub swd: c_ushort, + pub ftw: c_ushort, + pub fop: c_ushort, + pub rip: c_ulonglong, + pub rdp: c_ulonglong, + pub mxcsr: c_uint, + pub mxcr_mask: c_uint, + pub st_space: [c_uint; 32], + pub xmm_space: [c_uint; 64], + pub padding: [c_uint; 24], +} + + +#[repr(C)] +#[cfg(any(target_arch = "x86_64"))] +pub struct Regs { + pub r15: c_ulonglong, + pub r14: c_ulonglong, + pub r13: c_ulonglong, + pub r12: c_ulonglong, + pub rbp: c_ulonglong, + pub rbx: c_ulonglong, + pub r11: c_ulonglong, + pub r10: c_ulonglong, + pub r9: c_ulonglong, + pub r8: c_ulonglong, + pub rax: c_ulonglong, + pub rcx: c_ulonglong, + pub rdx: c_ulonglong, + pub rsi: c_ulonglong, + pub rdi: c_ulonglong, + pub orig_rax: c_ulonglong, + pub rip: c_ulonglong, + pub cs: c_ulonglong, + pub eflags: c_ulonglong, + pub rsp: c_ulonglong, + pub ss: c_ulonglong, + pub fs_base: c_ulonglong, + pub gs_base: c_ulonglong, + pub ds: c_ulonglong, + pub es: c_ulonglong, + pub fs: c_ulonglong, + pub gs: c_ulonglong, +} + + +#[repr(C)] +#[cfg(any(target_arch = "x86_64"))] +pub struct User { + regs: Regs, + u_fpvalid: c_int, + i387: FpRegs, + u_tsize: c_ulonglong, + u_dsize: c_ulonglong, + u_ssize: c_ulonglong, + start_code: c_ulonglong, + start_stack: c_ulonglong, + signal: c_longlong, + reserved: c_int, + u_ar0: *mut Regs, + u_fpstate: *mut FpRegs, + magic: c_ulonglong, + u_comm: [c_char; 32], + u_debugreg: [c_ulonglong; 8] +} + + + +#[repr(C)] +#[cfg(not(any(target_arch = "x86_64")))] +pub struct FpRegs { + pub cwd: c_long, + pub swd: c_long, + pub ftw: c_long, + pub fip: c_long, + pub fcs: c_long, + pub foo: c_long, + pub fos: c_long, + pub st_space: [c_long; 20], +} + + +#[repr(C)] +#[cfg(not(any(target_arch = "x86_64")))] +pub struct FpxRegs { + pub cwd: c_ushort, + pub swd: c_ushort, + pub twd: c_ushort, + pub fop: c_ushort, + pub fip: c_long, + pub fcs: c_long, + pub foo: c_long, + pub fos: c_long, + pub mxcsr: c_long, + pub reserved: c_long, + pub st_space: [c_long; 32], + pub xmm_space: [c_long; 32], + pub padding: [c_long; 56], +} + +#[repr(C)] +#[cfg(not(any(target_arch = "x86_64")))] +pub struct Regs { + pub ebx: c_long, + pub ecx: c_long, + pub edx: c_long, + pub esi: c_long, + pub edi: c_long, + pub ebp: c_long, + pub eax: c_long, + pub xds: c_long, + pub xes: c_long, + pub xfs: c_long, + pub xgs: c_long, + pub orig_eax: c_long, + pub eip: c_long, + pub xcs: c_long, + pub eflags: c_long, + pub esp: c_long, + pub xss: c_long, +} + + +#[repr(C)] +#[cfg(not(any(target_arch = "x86_64")))] +pub struct User { + regs: Regs, + u_fpvalid: c_int, + i387: FpRegs, + u_tsize: c_ulong, + u_dsize: c_ulong, + u_ssize: c_ulong, + start_code: c_ulong, + start_stack: c_ulong, + signal: c_long, + reserved: c_int, + u_ar0: *mut Regs, + u_fpstate: *mut FpRegs, + magic: c_ulong, + u_comm: [c_char; 32], + u_debugreg: [c_int; 8], +} From 1124448df88ff23d6dce830547544f9ee6e0fbb6 Mon Sep 17 00:00:00 2001 From: Daniel McKenna Date: Mon, 22 May 2017 21:16:43 +0100 Subject: [PATCH 2/5] Added definitions of EFlags. EFlags (Extended Flags) represents different flags that can be raised during execution. Implemented constants representing the possible flags as a bitflag and placed into original register structs. --- src/sys/user.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/sys/user.rs b/src/sys/user.rs index 6f027311bc..cfeec0f916 100644 --- a/src/sys/user.rs +++ b/src/sys/user.rs @@ -6,6 +6,28 @@ use libc::{c_ushort, c_uint, c_char, c_int, c_longlong, c_ulonglong}; use libc::{c_ushort, c_uint, c_char, c_int, c_long, c_ulong}; +libc_bitflags! { + pub flags ExtendedFlags: libc::size_t{ + EFLAG_CARRY = 1, + EFLAG_PARITY = 3, + EFLAG_AUXILIARY_CARRY = 5, + EFLAG_ZERO = 7, + EFLAG_SIGN = 8, + EFLAG_TRAP = 9, + EFLAG_INTERRUPT_ENABLE = 10, + EFLAG_DIRECTION = 11, + EFLAG_OVERFLOW = 12, + EFLAG_IOPRIVILEGE_1 = 13, + EFLAG_IOPRIVILEGE_2 = 14, + EFLAG_NESTEDTASK = 15, + EFLAG_RESUME = 17, + EFLAG_VIRTUAL_80086_MODE = 18, + EFLAG_ALIGNMENT_CHECK = 19, + EFLAG_VIRTUAL_INTERRUPT = 20, + EFLAG_VIRTUAL_INTERRUPT_PENDING = 21, + EFLAG_CPUID = 22 + } +} #[repr(C)] #[cfg(any(target_arch = "x86_64"))] pub struct FpRegs { @@ -44,7 +66,7 @@ pub struct Regs { pub orig_rax: c_ulonglong, pub rip: c_ulonglong, pub cs: c_ulonglong, - pub eflags: c_ulonglong, + pub eflags: ExtendedFlags, pub rsp: c_ulonglong, pub ss: c_ulonglong, pub fs_base: c_ulonglong, @@ -127,7 +149,7 @@ pub struct Regs { pub orig_eax: c_long, pub eip: c_long, pub xcs: c_long, - pub eflags: c_long, + pub eflags: ExtendedFlags, pub esp: c_long, pub xss: c_long, } From dd3f588b06f430488baf81b784c75890657bcc76 Mon Sep 17 00:00:00 2001 From: Daniel McKenna Date: Mon, 22 May 2017 21:26:51 +0100 Subject: [PATCH 3/5] Updated change log and cfg for user in mod.rs. Updated the changelog to reflect addition of sys/user and updated the conditional compilation to only include x86 and x86_64 and not limit based on OS. --- CHANGELOG.md | 2 ++ src/sys/mod.rs | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d59d217bd..fd037ef159 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#551](https://github.com/nix-rust/nix/pull/551)) - Added `nix::pty::{grantpt, posix_openpt, ptsname/ptsname_r, unlockpt}` ([#556](https://github.com/nix-rust/nix/pull/556) +- Added `nix::sys::user` + ([#TBD](https://github.com/nix-rust/nix/pull/) ### Changed - Marked `sys::mman::{ mmap, munmap, madvise, munlock, msync }` as unsafe. diff --git a/src/sys/mod.rs b/src/sys/mod.rs index a7bd2a2441..f50578beb7 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -59,7 +59,9 @@ pub mod time; )] pub mod ptrace; -#[cfg(all(target_os = "linux"))] +#[cfg(any(target_arch = "x86", + target_arch = "x86_64" + )] pub mod user; pub mod select; From 01811b9413dbd6d5432e8087302fb4d0c32b5447 Mon Sep 17 00:00:00 2001 From: Daniel McKenna Date: Mon, 22 May 2017 22:40:14 +0100 Subject: [PATCH 4/5] Fixed misuse of libc_bitflag and missing close bracket in cfg macro. --- src/sys/mod.rs | 2 +- src/sys/user.rs | 49 +++++++++++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/sys/mod.rs b/src/sys/mod.rs index f50578beb7..02a8f468f9 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -60,7 +60,7 @@ pub mod time; pub mod ptrace; #[cfg(any(target_arch = "x86", - target_arch = "x86_64" + target_arch = "x86_64") )] pub mod user; diff --git a/src/sys/user.rs b/src/sys/user.rs index cfeec0f916..ace4092cae 100644 --- a/src/sys/user.rs +++ b/src/sys/user.rs @@ -5,29 +5,30 @@ use libc::{c_ushort, c_uint, c_char, c_int, c_longlong, c_ulonglong}; #[cfg(any(target_arch = "x86"))] use libc::{c_ushort, c_uint, c_char, c_int, c_long, c_ulong}; +use libc::size_t; + +pub type Eflag = size_t; + +const EFLAG_CARRY: Eflag = 0x1; +const EFLAG_PARITY: Eflag = 0x1 << 2; +const EFLAG_AUXILIARY_CARRY: Eflag = 0x1 << 4; +const EFLAG_ZERO: Eflag = 0x1 << 6; +const EFLAG_SIGN: Eflag = 0x1 << 7; +const EFLAG_TRAP: Eflag = 0x1 << 8; +const EFLAG_INTERRUPT_ENABLE: Eflag = 0x1 << 9; +const EFLAG_DIRECTION: Eflag = 0x1 << 10; +const EFLAG_OVERFLOW: Eflag = 0x1 << 11; +const EFLAG_IOPRIVILEGE_1: Eflag = 0x1 << 12; +const EFLAG_IOPRIVILEGE_2: Eflag = 0x1 << 13; +const EFLAG_NESTEDTASK: Eflag = 0x1 << 14; +const EFLAG_RESUME: Eflag = 0x1 << 16; +const EFLAG_VIRTUAL_80086_MODE: Eflag = 0x1 << 17; +const EFLAG_ALIGNMENT_CHECK: Eflag = 0x1 << 18; +const EFLAG_VIRTUAL_INTERRUPT: Eflag = 0x1 << 19; +const EFLAG_VIRTUAL_INTERRUPT_PENDING: Eflag = 0x1 << 20; +const EFLAG_CPUID: Eflag = 0x1 << 21; + -libc_bitflags! { - pub flags ExtendedFlags: libc::size_t{ - EFLAG_CARRY = 1, - EFLAG_PARITY = 3, - EFLAG_AUXILIARY_CARRY = 5, - EFLAG_ZERO = 7, - EFLAG_SIGN = 8, - EFLAG_TRAP = 9, - EFLAG_INTERRUPT_ENABLE = 10, - EFLAG_DIRECTION = 11, - EFLAG_OVERFLOW = 12, - EFLAG_IOPRIVILEGE_1 = 13, - EFLAG_IOPRIVILEGE_2 = 14, - EFLAG_NESTEDTASK = 15, - EFLAG_RESUME = 17, - EFLAG_VIRTUAL_80086_MODE = 18, - EFLAG_ALIGNMENT_CHECK = 19, - EFLAG_VIRTUAL_INTERRUPT = 20, - EFLAG_VIRTUAL_INTERRUPT_PENDING = 21, - EFLAG_CPUID = 22 - } -} #[repr(C)] #[cfg(any(target_arch = "x86_64"))] pub struct FpRegs { @@ -66,7 +67,7 @@ pub struct Regs { pub orig_rax: c_ulonglong, pub rip: c_ulonglong, pub cs: c_ulonglong, - pub eflags: ExtendedFlags, + pub eflags: Eflag, pub rsp: c_ulonglong, pub ss: c_ulonglong, pub fs_base: c_ulonglong, @@ -149,7 +150,7 @@ pub struct Regs { pub orig_eax: c_long, pub eip: c_long, pub xcs: c_long, - pub eflags: ExtendedFlags, + pub eflags: Eflag, pub esp: c_long, pub xss: c_long, } From 0727c18ae26eb7503b77edeb82f003583fdf953f Mon Sep 17 00:00:00 2001 From: Daniel McKenna Date: Mon, 22 May 2017 23:11:13 +0100 Subject: [PATCH 5/5] Fixed unused import and updated pull request number in CHANGELOG. --- CHANGELOG.md | 2 +- src/sys/user.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd037ef159..3bddf62a0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Added `nix::pty::{grantpt, posix_openpt, ptsname/ptsname_r, unlockpt}` ([#556](https://github.com/nix-rust/nix/pull/556) - Added `nix::sys::user` - ([#TBD](https://github.com/nix-rust/nix/pull/) + ([#606](https://github.com/nix-rust/nix/pull/606) ### Changed - Marked `sys::mman::{ mmap, munmap, madvise, munlock, msync }` as unsafe. diff --git a/src/sys/user.rs b/src/sys/user.rs index ace4092cae..046e4c6c1a 100644 --- a/src/sys/user.rs +++ b/src/sys/user.rs @@ -3,7 +3,7 @@ #[cfg(any(target_arch = "x86_64"))] use libc::{c_ushort, c_uint, c_char, c_int, c_longlong, c_ulonglong}; #[cfg(any(target_arch = "x86"))] -use libc::{c_ushort, c_uint, c_char, c_int, c_long, c_ulong}; +use libc::{c_ushort, c_char, c_int, c_long, c_ulong}; use libc::size_t;