Skip to content

Commit d4a63a6

Browse files
author
cyh21
committed
2025-3-20
1 parent eaa2ae9 commit d4a63a6

7 files changed

Lines changed: 374 additions & 0 deletions

File tree

_sidebar.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,9 @@
3636
* [2025-3-13](./docs/日报/2025-3-13.md)
3737
* [2025-3-15](./docs/日报/2025-3-15.md)
3838
* [2025-3-16](./docs/日报/2025-3-16.md)
39+
* [2025-3-17](./docs/日报/2025-3-17.md)
40+
* [2025-3-18](./docs/日报/2025-3-18.md)
41+
* [2025-3-19](./docs/日报/2025-3-19.md)
42+
* [2025-3-20](./docs/日报/2025-3-20.md)
3943

4044

docs/日报/2025-3-16.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,78 @@ pub fn syscall_prlimit64(args: [usize; 6]) -> SyscallResult {
6464
}
6565
```
6666

67+
- DragonOS
68+
```
69+
// # 设置资源限制
70+
///
71+
/// TODO: 目前暂时不支持设置资源限制,只提供读取默认值的功能
72+
///
73+
/// ## 参数
74+
///
75+
/// - pid: 进程号
76+
/// - resource: 资源类型
77+
/// - new_limit: 新的资源限制
78+
/// - old_limit: 旧的资源限制
79+
///
80+
/// ## 返回值
81+
///
82+
/// - 成功,0
83+
/// - 如果old_limit不为NULL,则返回旧的资源限制到old_limit
84+
///
85+
pub fn prlimit64(
86+
_pid: Pid,
87+
resource: usize,
88+
_new_limit: *const RLimit64,
89+
old_limit: *mut RLimit64,
90+
) -> Result<usize, SystemError> {
91+
let resource = RLimitID::try_from(resource)?;
92+
let mut writer = None;
93+
94+
if !old_limit.is_null() {
95+
writer = Some(UserBufferWriter::new(
96+
old_limit,
97+
core::mem::size_of::<RLimit64>(),
98+
true,
99+
)?);
100+
}
101+
102+
match resource {
103+
RLimitID::Stack => {
104+
if let Some(mut writer) = writer {
105+
let mut rlimit = writer.buffer::<RLimit64>(0).unwrap()[0];
106+
rlimit.rlim_cur = UserStack::DEFAULT_USER_STACK_SIZE as u64;
107+
rlimit.rlim_max = UserStack::DEFAULT_USER_STACK_SIZE as u64;
108+
}
109+
return Ok(0);
110+
}
111+
112+
RLimitID::Nofile => {
113+
if let Some(mut writer) = writer {
114+
let mut rlimit = writer.buffer::<RLimit64>(0).unwrap()[0];
115+
rlimit.rlim_cur = FileDescriptorVec::PROCESS_MAX_FD as u64;
116+
rlimit.rlim_max = FileDescriptorVec::PROCESS_MAX_FD as u64;
117+
}
118+
return Ok(0);
119+
}
120+
121+
RLimitID::As | RLimitID::Rss => {
122+
if let Some(mut writer) = writer {
123+
let mut rlimit = writer.buffer::<RLimit64>(0).unwrap()[0];
124+
rlimit.rlim_cur = MMArch::USER_END_VADDR.data() as u64;
125+
rlimit.rlim_max = MMArch::USER_END_VADDR.data() as u64;
126+
}
127+
return Ok(0);
128+
}
129+
130+
_ => {
131+
return Err(SystemError::ENOSYS);
132+
}
133+
}
134+
}
135+
```
136+
137+
138+
67139
- 使用方式
68140
- 测例:argv
69141
- prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0

docs/日报/2025-3-17.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# prlimit64实现
2+
3+
增加syscall
4+
5+
![alt text](image-5.png)
6+
7+
在ctype中加入参数
8+
```rust
9+
/// sys_prlimit64 使用的数组
10+
#[repr(C)]
11+
#[derive(Debug, Copy, Clone)]
12+
pub struct RLimit {
13+
pub rlim_cur: u64, // 当前软限制
14+
pub rlim_max: u64, // 最大硬限制
15+
}
16+
// sys_prlimit64 使用的选项
17+
/// 用户栈大小
18+
pub const RLIMIT_STACK: i32 = 3;
19+
/// 可以打开的 fd 数
20+
pub const RLIMIT_NOFILE: i32 = 7;
21+
/// 用户地址空间的最大大小
22+
pub const RLIMIT_AS: i32 = 9;
23+
```
24+
25+
26+
实现(目前只尝试STACK_SIZE)
27+
```rust
28+
#[apply(syscall_instrument)]
29+
pub fn sys_prlimit64(
30+
pid: i32,
31+
resource: i32,
32+
new_limit: UserConstPtr<RLimit>,
33+
old_limit: UserPtr<RLimit>,
34+
) -> LinuxResult<isize> {
35+
// 检查资源类型是否有效
36+
// let curr_process = current().task_ext_mut();
37+
let curr_process = current();
38+
let task_ext = curr_process.task_ext();
39+
if pid == 0 || pid == task_ext.proc_id as i32 {
40+
// 仅支持当前进程
41+
match resource {
42+
// RLIMIT_AS => {
43+
// let new_limit = new_limit.get()?;
44+
// let old_limit = old_limit.get_mut()?;
45+
// let old_limit = curr_process.task_ext().set_rlimit(RLIMIT_AS, new_limit, old_limit);
46+
// Ok(0)
47+
// }
48+
RLIMIT_STACK => {
49+
let new_limit = new_limit.get()?;
50+
let old_limit = old_limit.get()?;
51+
// let old_limit = curr_process.task_ext().set_rlimit(RLIMIT_STACK, new_limit, old_limit);
52+
// Ok(0)
53+
// let mut stack_limit = curr_process
54+
let mut stack_limit: u64 = task_ext.get_stack_size();
55+
if old_limit as usize != 0 {
56+
unsafe {
57+
*old_limit = RLimit {
58+
rlim_cur: stack_limit,
59+
rlim_max: stack_limit,
60+
};
61+
}
62+
}
63+
if new_limit as usize != 0 {
64+
stack_limit = unsafe {
65+
(*new_limit).rlim_cur
66+
};
67+
task_ext.set_stack_size(stack_limit);
68+
}
69+
}
70+
// RLIMIT_NOFILE => {
71+
// let new_limit = new_limit.get()?;
72+
// let old_limit = old_limit.get_mut()?;
73+
// let old_limit = curr_process.task_ext().set_rlimit(RLIMIT_NOFILE, new_limit, old_limit);
74+
// Ok(0)
75+
// }
76+
// _ => Err(LinuxError::EINVAL),
77+
_=> { }
78+
}
79+
} else {
80+
return Err(LinuxError::EINVAL);
81+
}
82+
83+
Ok(0)
84+
}
85+
```
86+

docs/日报/2025-3-18.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# 使用qemu运行不同指令集的架构镜像(以argv为例)
2+
3+
将镜像文件拷贝到文件夹下,执行以下命令:
4+
```
5+
mkdir -p mnt
6+
sudo mount xxx.img mnt
7+
```
8+
再进入mnt/musl
9+
10+
## riscv 架构
11+
```shell
12+
qemu-riscv64 -strace ./runtest.exe -w entry-static.exe argv ls
13+
```
14+
15+
```shell
16+
14092 set_tid_address(0x1d238) = 14092
17+
14092 rt_sigprocmask(SIG_BLOCK,0x00002aaaab2aa7a0,NULL,8) = 0
18+
14092 rt_sigprocmask(SIG_UNBLOCK,0x00002aaaab2aa5e0,NULL,8) = 0
19+
14092 rt_sigaction(SIGCHLD,0x00002aaaab2aa5c0,0x00002aaaab2aa5e0) = 0
20+
14092 write(1,0xab2aa530,50)========== START entry-static.exe argv ==========
21+
= 50
22+
14092 rt_sigprocmask(SIG_BLOCK,0x000000000001d160,0x00002aaaab2aa6f0,8) = 0
23+
14092 clone(0x11,child_stack=0x0000000000000000,parent_tidptr=0x00002aaaab2aa6f0,tls=0x0000000000000008,child_tidptr=0x0000000000000000) = 14094
24+
= 0
25+
14092 rt_sigprocmask(SIG_SETMASK,0x00002aaaab2aa6f0,NULL,8) = 014094
26+
gettid() = 14094
27+
14094 rt_sigprocmask(SIG_SETMASK,0x00002aaaab2aa6f0,NULL,8) = 0
28+
14092 rt_sigtimedwait(46912504506272,0,46912504506256,8,0,0)14094 prlimit64(0,RLIMIT_STACK,NULL,0x00002aaaab2aa750) = 0 ({rlim_cur=8388608,rlim_max=-1})
29+
14094 rt_sigprocmask(SIG_BLOCK,0x000000000001d168,0x00002aaaab2aa5b8,8) = 0
30+
14094 rt_sigprocmask(SIG_BLOCK,0x000000000001d160,NULL,8) = 0
31+
14094 prlimit64(0,RLIMIT_STACK,{rlim_cur=102400,rlim_max=102400},NULL) = 0
32+
14094 rt_sigprocmask(SIG_SETMASK,0x00002aaaab2aa5b8,NULL,8) = 0
33+
14094 execve("entry-static.exe",{"entry-static.exe","argv","ls",NULL}) = -1 errno=8 (Exec format error)
34+
14094 write(1,0xab2aa530,73)src/common/runtest.c:29: entry-static.exe exec failed: Exec format error
35+
= 73
36+
14094 exit_group(1)
37+
= 17
38+
14092 wait4(14094,0x2aaaab2aa790,0,(nil)) = 14094
39+
14092 write(1,0xab2aa530,21)FAIL argv [status 1]
40+
= 21
41+
14092 write(1,0xab2aa530,48)========== END entry-static.exe argv ==========
42+
= 48
43+
14092 exit_group(1)
44+
```
45+
46+
## loongarch 架构
47+
```shell
48+
qemu-loongarch64 -strace ./runtest.exe -w entry-static.exe argv ls
49+
```
50+
51+
```shell
52+
21399 set_tid_address(0x120014920) = 21399
53+
21399 rt_sigprocmask(SIG_BLOCK,0x0000555555d55780,NULL,8) = 0
54+
21399 rt_sigprocmask(SIG_UNBLOCK,0x0000555555d555d0,NULL,8) = 0
55+
21399 rt_sigaction(SIGCHLD,0x0000555555d555b0,0x0000555555d555d0) = 0
56+
21399 write(1,0x55d55510,50)========== START entry-static.exe argv ==========
57+
= 50
58+
21399 rt_sigprocmask(SIG_BLOCK,0x000000012000dc98,0x0000555555d556a0,8) = 0
59+
21399 rt_sigprocmask(SIG_BLOCK,0x000000012000dca0,0x0000555555d55610,8) = 0
60+
21399 clone(0x11,child_stack=0x0000000000000000,parent_tidptr=0x0000555555d55610,tls=0x0000000000000000,child_tidptr=0x0000000000000008) = 21401
61+
= 21399 rt_sigprocmask(SIG_SETMASK,00x0000555555d55610,
62+
NULL,8) = 0
63+
21399 21401 rt_sigprocmask(set_tid_address(0x120014920)SIG_SETMASK,0x0000555555d556a0,NULL,8) = 0
64+
= 21401
65+
21399 rt_sigtimedwait(93825000626048,0,93825000626032,8,0,0)21401 rt_sigprocmask(SIG_SETMASK,0x0000555555d55610,NULL,8) = 0
66+
21401 rt_sigprocmask(SIG_SETMASK,0x0000555555d556a0,NULL,8) = 0
67+
21401 prlimit64(0,RLIMIT_STACK,NULL,0x0000555555d55730) = 0 ({rlim_cur=8388608,rlim_max=-1})
68+
21401 prlimit64(0,RLIMIT_STACK,{rlim_cur=102400,rlim_max=102400},NULL) = 0
69+
21401 execve("entry-static.exe",{"entry-static.exe","argv","ls",NULL}) = -1 errno=8 (Exec format error)
70+
21401 write(1,0x55d55510,73)src/common/runtest.c:29: entry-static.exe exec failed: Exec format error
71+
= 73
72+
21401 exit_group(1)
73+
= 17
74+
21399 wait4(21401,0x555555d55770,0,(nil)) = 21401
75+
21399 write(1,0x55d55510,21)FAIL argv [status 1]
76+
= 21
77+
21399 write(1,0x55d55510,48)========== END entry-static.exe argv ==========
78+
= 48
79+
21399 exit_group(1)
80+
```
81+
82+
## aarch64 架构
83+
```shell
84+
qemu-aarch64 -strace ./runtest.exe -w entry-static.exe argv ls
85+
```
86+
87+
```shell
88+
23575 set_tid_address(0x41d7f8) = 23575
89+
23575 rt_sigprocmask(SIG_BLOCK,0x00004000007ff7c0,NULL,8) = 0
90+
23575 rt_sigprocmask(SIG_UNBLOCK,0x00004000007ff5c0,NULL,8) = 0
91+
23575 rt_sigaction(SIGCHLD,0x00004000007ff5a0,0x00004000007ff5c0) = 0
92+
23575 write(1,0x7ff4a0,50)========== START entry-static.exe argv ==========
93+
= 50
94+
23575 rt_sigprocmask(SIG_BLOCK,0x000000000040bc28,0x00004000007ff6e0,8) = 0
95+
23575 rt_sigprocmask(SIG_BLOCK,0x000000000040bc20,0x00004000007ff620,8) = 0
96+
23575 clone(0x11,child_stack=0x0000000000000000,parent_tidptr=0x0000000000000000,tls=0x0000000000000008,child_tidptr=0x0000000000000000) = 23577
97+
= 0
98+
23575 rt_sigprocmask(SIG_SETMASK,0x00004000007ff620,NULL,8) = 023577
99+
gettid() = 23577
100+
23577 rt_sigprocmask(SIG_SETMASK,0x00004000007ff620,NULL,8) = 0
101+
23577 23575 rt_sigprocmask(rt_sigprocmask(SIG_SETMASK,SIG_SETMASK,0x00004000007ff6e0,0x00004000007ff6e0,NULL,NULL,88)) = 0 =
102+
0
103+
23577 prlimit64(0,RLIMIT_STACK,NULL,0x00004000007ff750) = 0 ({rlim_cur=8388608,rlim_max=-1})
104+
23575 rt_sigtimedwait(70368752564160,0,70368752564144,8,0,0)23577 prlimit64(0,RLIMIT_STACK,{rlim_cur=102400,rlim_max=102400},NULL) = 0
105+
23577 execve("entry-static.exe",{"entry-static.exe","argv","ls",NULL}) = -1 errno=8 (Exec format error)
106+
23577 write(1,0x7ff4a0,73)src/common/runtest.c:29: entry-static.exe exec failed: Exec format error
107+
= 73
108+
23577 exit_group(1)
109+
= 17
110+
23575 wait4(23577,0x4000007ff7b0,0,(nil)) = 23577
111+
23575 write(1,0x7ff4a0,21)FAIL argv [status 1]
112+
= 21
113+
23575 write(1,0x7ff4a0,48)========== END entry-static.exe argv ==========
114+
= 48
115+
23575 exit_group(1)
116+
```

docs/日报/2025-3-19.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 为prlimit64编写测例
2+
3+
## 测试代码
4+
```c
5+
#include <stdio.h>
6+
#include <sys/resource.h>
7+
#include <unistd.h>
8+
#include <sys/syscall.h>
9+
10+
int main() {
11+
struct rlimit old_limit, new_limit;
12+
13+
// 获取当前进程的栈大小限制
14+
if (syscall(SYS_prlimit64, getpid(), RLIMIT_STACK, NULL, &old_limit) == -1) {
15+
perror("prlimit64 get stack limit failed");
16+
return 1;
17+
}
18+
printf("Current STACK limits: soft=%llu, hard=%llu\n", (unsigned long long)old_limit.rlim_cur, (unsigned long long)old_limit.rlim_max);
19+
20+
// 设置新的栈大小限制
21+
new_limit.rlim_cur = 8 * 1024 * 1024; // 8MB
22+
new_limit.rlim_max = 16 * 1024 * 1024; // 16MB
23+
if (syscall(SYS_prlimit64, getpid(), RLIMIT_STACK, &new_limit, NULL) == -1) {
24+
perror("prlimit64 set new stack limit failed");
25+
return 1;
26+
}
27+
printf("Set new STACK limits: soft=%llu, hard=%llu\n", (unsigned long long)new_limit.rlim_cur, (unsigned long long)new_limit.rlim_max);
28+
29+
// 再次获取栈大小限制,验证是否设置成功
30+
if (syscall(SYS_prlimit64, getpid(), RLIMIT_STACK, NULL, &old_limit) == -1) {
31+
perror("prlimit64 get new stack limit failed");
32+
return 1;
33+
}
34+
printf("New STACK limits: soft=%llu, hard=%llu\n", (unsigned long long)old_limit.rlim_cur, (unsigned long long)old_limit.rlim_max);
35+
36+
return 0;
37+
}
38+
```
39+
40+
41+
## strace结果
42+
```shell
43+
cyh@LAPTOP-D52U729F:~/starry-next$ qemu-riscv64 -strace ./apps/tests/build/prlimit64
44+
12038 set_tid_address(0x55555555e778) = 12038
45+
12038 getpid() = 12038
46+
12038 prlimit64(12038,RLIMIT_STACK,NULL,0x00002aaaab2aa640) = 0 ({rlim_cur=8388608,rlim_max=-1})
47+
12038 ioctl(1,TIOCGWINSZ,0x00002aaaab2aa308) = 0 ({22,139,0,0})
48+
12038 writev(1,0x2aaaab2aa2b0,0x2)Current STACK limits: soft=8388608, hard=18446744073709551615
49+
= 62
50+
12038 getpid() = 12038
51+
12038 prlimit64(12038,RLIMIT_STACK,{rlim_cur=8388608,rlim_max=16777216},NULL) = 0
52+
12038 writev(1,0x2aaaab2aa2d0,0x2)Set new STACK limits: soft=8388608, hard=16777216
53+
= 50
54+
12038 getpid() = 12038
55+
12038 prlimit64(12038,RLIMIT_STACK,NULL,0x00002aaaab2aa640) = 0 ({rlim_cur=8388608,rlim_max=-1})
56+
12038 writev(1,0x2aaaab2aa2d0,0x2)New STACK limits: soft=8388608, hard=18446744073709551615
57+
= 58
58+
12038 exit_group(0)
59+
```

docs/日报/2025-3-20.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# 运行测例
2+
3+
## 流程
4+
5+
- 在apps下创建tests文件夹
6+
- 在tests文件夹下创建并编写prlimit64.c文件
7+
- tests下创建build文件夹
8+
- 在tests文件夹下创建testcase_list文件,添加测例
9+
- tests下执行riscv64-linux-musl-gcc -static apps/tests/prlimit64.c -o apps/tests/build/prlimit64
10+
- starry-next下执行sudo ./build_img.sh -a x86_64 -fs ext4 -file apps/tests/build/ -s 30
11+
- make AX_TESTCASE=tests ARCH=riscv64 EXTRA_CONFIG=../configs/riscv64.toml BLK=y NET=y SMP=4 FEATURES=fp_simd,lwext4_rs LOG=info run
12+
13+
## 测试结果(失败,正在调试)
14+
```shell
15+
[ 6.203695 0:10 starry::syscall_imp:57] Syscall set_tid_address
16+
[ 6.233718 0:10 starry::syscall_imp:170] Syscall set_tid_address return 10
17+
[ 6.244388 0:10 starry::syscall_imp:57] Syscall getpid
18+
[ 6.250047 0:10 starry::syscall_imp:170] Syscall getpid return 10
19+
[ 6.256574 0:10 starry::syscall_imp:57] Syscall prlimit64
20+
[ 6.266883 0:10 starry::syscall_imp::task::thread:213] sys_prlimit64 => Err(EFAULT)
21+
[ 6.277822 0:10 starry::syscall_imp:170] Syscall prlimit64 return -14
22+
[ 6.288521 0:10 starry::syscall_imp:57] Syscall writev
23+
prlimit64 get stack limit failed[ 6.318049 0:10 starry::syscall_imp:170] Syscall writev return 32
24+
[ 6.326058 0:10 starry::syscall_imp:57] Syscall writev
25+
:[ 6.330115 0:10 starry::syscall_imp:170] Syscall writev return 1
26+
[ 6.335907 0:10 starry::syscall_imp:57] Syscall writev
27+
[ 6.340294 0:10 starry::syscall_imp:170] Syscall writev return 1
28+
[ 6.347025 0:10 starry::syscall_imp:57] Syscall writev
29+
Bad address[ 6.352396 0:10 starry::syscall_imp:170] Syscall writev return 11
30+
[ 6.358162 0:10 starry::syscall_imp:57] Syscall writev
31+
32+
[ 6.364011 0:10 starry::syscall_imp:170] Syscall writev return 1
33+
[ 6.372136 0:10 starry::syscall_imp:57] Syscall exit_group
34+
[ 6.377821 0:10 starry::syscall_imp::task::thread:62] Temporarily replace sys_exit_group with sys_exit
35+
[ 6.407838 1:2 starry:59] User task /prlimit64 exited with code: Some(1)
36+
[ 6.428410 1:2 axhal::platform::riscv64_qemu_virt::misc:3] Shutting down...
37+
```

docs/日报/image-5.png

39.6 KB
Loading

0 commit comments

Comments
 (0)