summary refs log tree commit diff
path: root/ripple/minitrace/src/syscall_abi/mod.rs
blob: 31fd2e756e7d9d1397f99fce656bf66311170411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-FileCopyrightText: edef <edef@unfathomable.blue>
// SPDX-FileCopyrightText: V <v@unfathomable.blue>
// SPDX-License-Identifier: OSL-3.0

use {
	crate::{define_syscalls, syscall_bitflags, syscall_enums, Process},
	std::{
		ffi::CString,
		fmt::{self, Debug, Write},
	},
};

pub(crate) use {
	arg::{ProcessSyscallArg, SyscallArg},
	device::Device,
	fd::{DirFd, FileDesc},
};

mod arg;
mod device;
mod fd;
pub mod macros;

type SigAction = ();
type SysInfo = ();
type Tms = ();
type Stat = ();
type RobustListHead = ();
type RLimit64 = ();

define_syscalls! {
	enum SyscallEntry {
		fn read(fd: FileDesc, buf: *mut u8, count: usize) -> i64 = 0;
		fn write(fd: FileDesc, buf: *const u8, count: usize) -> i64 = 1;
		fn close(fd: FileDesc) -> i64 = 3;
		fn mmap(addr: u64, len: u64, prot: ProtFlags, flags: MapFlags, fd: Option<FileDesc>, off: u64) -> i64 = 9;
		fn mprotect(addr: u64, len: usize, prot: ProtFlags) -> i64 = 10;
		fn brk(brk: u64) -> i64 = 12;
		fn rt_sigaction(sig: Signal, act: *const SigAction, oact: *mut SigAction, sigsetsize: SigSetSize) -> i64 = 13;
		fn ioctl(fd: FileDesc, cmd: Ioctl, arg: u64) -> i64 = 16;
		fn pread64(fd: FileDesc, buf: *mut u8, count: usize, pos: u64) -> i64 = 17;
		fn access(filename: CString, mode: AccessMode) -> i64 = 21;
		fn getcwd(buf: *mut u8, size: u64) -> i64 = 79;
		fn readlink(path: CString, buf: *mut u8, bufsiz: i32) -> i64 = 89;
		fn sysinfo(info: *mut SysInfo) -> i64 = 99;
		fn times(tbuf: *mut Tms) -> i64 = 100;
		fn arch_prctl(option: ArchOption, arg2: u64) -> i64 = 158;
		fn set_tid_address(tidptr: *mut i32) -> i64 = 218;
		fn exit_group(error_code: i32) -> i64 = 231;
		fn openat(dfd: DirFd, filename: CString, flags: OpenFlags, mode: FileMode) -> i64 = 257;
		fn newfstatat(dfd: DirFd, filename: CString, statbuf: *mut Stat, flags: AtFlags) -> i64 = 262;
		fn set_robust_list(head: *mut RobustListHead, len: RobustListHeadSize) -> i64 = 273;
		fn prlimit64(pid: Zero, resource: ResourceLimit, new_rlim: *const RLimit64, old_rlim: *mut RLimit64) -> i64 = 302;
		fn getrandom(ubuf: *mut u8, len: usize, flags: GrndFlags) -> i64 = 318;
	}
}

syscall_bitflags! {
	struct OpenFlags: i32 {
		const WRONLY  = 1 <<  0 => O_WRONLY;
		const CREAT   = 1 <<  6 => O_CREAT;
		const NOCTTY  = 1 <<  8 => O_NOCTTY;
		const TRUNC   = 1 <<  9 => O_TRUNC;
		const CLOEXEC = 1 << 19 => O_CLOEXEC;
	}

	struct GrndFlags: u32 {
		const NONBLOCK = 1 << 0 => GRND_NONBLOCK;
		const RANDOM   = 1 << 1 => GRND_RANDOM;
	}

	struct MapFlags: i32 {
		const PRIVATE   = 1 <<  1 => MAP_PRIVATE;
		const FIXED     = 1 <<  4 => MAP_FIXED;
		const ANONYMOUS = 1 <<  5 => MAP_ANONYMOUS;
		const DENYWRITE = 1 << 11 => MAP_DENYWRITE;
	}

	struct ProtFlags: i32 {
		const READ  = 1 << 0 => PROT_READ;
		const WRITE = 1 << 1 => PROT_WRITE;
		const EXEC  = 1 << 2 => PROT_EXEC;
	}

	struct AtFlags: i32 {
		const EMPTY_PATH = 1 << 12 => AT_EMPTY_PATH;
	}

	struct AccessMode: i32 {
		const F = 0 => F_OK;
		const X = 1 << 0 => X_OK;
		const W = 1 << 1 => W_OK;
		const R = 1 << 2 => R_OK;
	}

	struct FileMode: u32 {
		const IRUSR = 0o400 => S_IRUSR;
		const IWUSR = 0o200 => S_IWUSR;
		const IXUSR = 0o100 => S_IXUSR;

		const IRGRP = 0o040 => S_IRGRP;
		const IWGRP = 0o020 => S_IWGRP;
		const IXGRP = 0o010 => S_IXGRP;

		const IROTH = 0o004 => S_IROTH;
		const IWOTH = 0o002 => S_IWOTH;
		const IXOTH = 0o001 => S_IXOTH;
	}
}

syscall_enums! {
	enum ResourceLimit: u32 {
		STACK = 0x3 => RLIMIT_STACK,
		RSS   = 0x5 => RLIMIT_RSS,
		AS    = 0x9 => RLIMIT_AS,
	}

	enum ArchOption: i32 {
		SET_FS = 0x1002,
	}

	enum Ioctl: u64 {
		TCGETS     = 0x5401 => TCGETS,
		TIOCGWINSZ = 0x5413 => TIOCGWINSZ,
	}

	enum Signal: i32 {
		ILL  =  4 => SIGILL,
		ABRT =  6 => SIGABRT,
		BUS  =  7 => SIGBUS,
		FPE  =  8 => SIGFPE,
		SEGV = 11 => SIGSEGV,
	}
}

#[derive(Copy, Clone)]
pub(crate) struct Zero;

impl Debug for Zero {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.write_char('0')
	}
}

impl SyscallArg for Zero {
	fn try_from_reg(reg: u64) -> Option<Self> {
		if reg == 0 {
			Some(Zero)
		} else {
			None
		}
	}
}

#[derive(Debug, Copy, Clone)]
pub(crate) struct SigSetSize;

impl SyscallArg for SigSetSize {
	fn try_from_reg(reg: u64) -> Option<Self> {
		if reg == 8 {
			Some(SigSetSize)
		} else {
			None
		}
	}
}

#[derive(Debug, Copy, Clone)]
pub(crate) struct RobustListHeadSize;

impl SyscallArg for RobustListHeadSize {
	fn try_from_reg(reg: u64) -> Option<Self> {
		if reg == 24 {
			Some(RobustListHeadSize)
		} else {
			None
		}
	}
}