Skip to content

Commit 27a6de6

Browse files
committed
initial ptrace
1 parent 626492c commit 27a6de6

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

etc/syscalls_linux_aarch64.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
| 0x72 (114) | clock_getres | (const clockid_t which_clock, struct __kernel_timespec *tp) | __arm64_sys_clock_getres | false |
118118
| 0x73 (115) | clock_nanosleep | (const clockid_t which_clock, int flags, const struct __kernel_timespec *rqtp, struct __kernel_timespec *rmtp) | __arm64_sys_clock_nanosleep | false |
119119
| 0x74 (116) | syslog | (int type, char *buf, int len) | __arm64_sys_syslog | false |
120-
| 0x75 (117) | ptrace | (long request, long pid, unsigned long addr, unsigned long data) | __arm64_sys_ptrace | false |
120+
| 0x75 (117) | ptrace | (long request, long pid, unsigned long addr, unsigned long data) | __arm64_sys_ptrace | partially |
121121
| 0x76 (118) | sched_setparam | (pid_t pid, struct sched_param *param) | __arm64_sys_sched_setparam | false |
122122
| 0x77 (119) | sched_setscheduler | (pid_t pid, int policy, struct sched_param *param) | __arm64_sys_sched_setscheduler | false |
123123
| 0x78 (120) | sched_getscheduler | (pid_t pid) | __arm64_sys_sched_getscheduler | false |

src/process/clone.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
sync::SpinLock,
99
};
1010
use alloc::boxed::Box;
11+
use core::sync::atomic::AtomicBool;
1112
use bitflags::bitflags;
1213
use libkernel::memory::address::TUA;
1314
use libkernel::{
@@ -153,6 +154,7 @@ pub async fn sys_clone(
153154
creds: SpinLock::new(creds),
154155
state: Arc::new(SpinLock::new(TaskState::Runnable)),
155156
last_cpu: SpinLock::new(CpuId::this()),
157+
ptrace: AtomicBool::new(flags.contains(CloneFlags::CLONE_PTRACE) && current_task.ptrace.load(core::sync::atomic::Ordering::SeqCst)),
156158
}),
157159
}
158160
};

src/process/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloc::{
44
sync::{Arc, Weak},
55
};
66
use core::fmt::Display;
7+
use core::sync::atomic::AtomicBool;
78
use creds::Credentials;
89
use fd_table::FileDescriptorTable;
910
use libkernel::{VirtualMemory, fs::Inode};
@@ -18,6 +19,7 @@ pub mod exec;
1819
pub mod exit;
1920
pub mod fd_table;
2021
pub mod owned;
22+
pub mod ptrace;
2123
pub mod sleep;
2224
pub mod thread_group;
2325
pub mod threading;
@@ -163,6 +165,7 @@ pub struct Task {
163165
pub fd_table: Arc<SpinLock<FileDescriptorTable>>,
164166
pub state: Arc<SpinLock<TaskState>>,
165167
pub last_cpu: SpinLock<CpuId>,
168+
pub ptrace: AtomicBool
166169
}
167170

168171
impl Task {

src/process/owned.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::{
1919
sync::SpinLock,
2020
};
2121
use alloc::sync::Arc;
22+
use core::sync::atomic::AtomicBool;
2223
use libkernel::{
2324
VirtualMemory,
2425
fs::pathbuf::PathBuf,
@@ -75,6 +76,7 @@ impl OwnedTask {
7576
vm: Arc::new(SpinLock::new(vm)),
7677
fd_table: Arc::new(SpinLock::new(FileDescriptorTable::new())),
7778
last_cpu: SpinLock::new(CpuId::this()),
79+
ptrace: AtomicBool::new(false),
7880
};
7981

8082
Self {
@@ -102,6 +104,7 @@ impl OwnedTask {
102104
)),
103105
fd_table: Arc::new(SpinLock::new(FileDescriptorTable::new())),
104106
last_cpu: SpinLock::new(CpuId::this()),
107+
ptrace: AtomicBool::new(false),
105108
};
106109

107110
Self {

src/process/ptrace.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use core::sync::atomic::Ordering;
2+
use libkernel::error::{KernelError, Result};
3+
use libkernel::memory::address::UA;
4+
use crate::process::Tid;
5+
use crate::sched::current::current_task_shared;
6+
7+
#[repr(i32)]
8+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
9+
enum PtraceOperation {
10+
TraceMe = 0,
11+
PeekText = 1,
12+
PeekData = 2,
13+
}
14+
15+
impl TryFrom<i32> for PtraceOperation {
16+
type Error = KernelError;
17+
18+
fn try_from(value: i32) -> Result<Self> {
19+
match value {
20+
0 => Ok(PtraceOperation::TraceMe),
21+
1 => Ok(PtraceOperation::PeekText),
22+
2 => Ok(PtraceOperation::PeekData),
23+
// TODO: Should be EIO
24+
_ => Err(KernelError::InvalidValue)
25+
}
26+
}
27+
}
28+
29+
pub async fn sys_ptrace(op: i32, pid: Tid, addr: UA, data: UA) -> Result<usize> {
30+
let op = PtraceOperation::try_from(op)?;
31+
match op {
32+
PtraceOperation::TraceMe => {
33+
current_task_shared().ptrace.store(true, Ordering::SeqCst);
34+
Ok(0)
35+
}
36+
_ => Err(KernelError::InvalidValue),
37+
}
38+
}

0 commit comments

Comments
 (0)