Skip to content

Commit 721dfbf

Browse files
BernardXiongRbb666
authored andcommitted
[smart] Add vDSO support for RISC-V architecture and refactor related components
- Implemented vDSO functionality for the RISC-V architecture, including the necessary source files and linker scripts. - Introduced a new `vdso_sys.c` file for RISC-V, containing functions to handle time retrieval using the vDSO mechanism. - Created architecture-specific linker scripts (`vdso.lds.S`) for both AArch64 and RISC-V. - Updated the build system to support vDSO compilation for RISC-V, including necessary adjustments in `SConstruct` files. - Refactored existing vDSO code to improve compatibility and maintainability across architectures. - Adjusted the maximum number of PTY devices in the terminal configuration from 64 to 32 for better resource management. - Fixed minor issues in existing code, including correcting the path for the vDSO shared library and ensuring proper function definitions.
1 parent 2a525e8 commit 721dfbf

File tree

21 files changed

+473
-266
lines changed

21 files changed

+473
-266
lines changed

components/lwp/Kconfig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
menuconfig RT_USING_LWP
2-
bool "light-weight process"
2+
bool "lwP(light weight Process)"
33
depends on RT_USING_SMART
44
default y
55
help
@@ -87,4 +87,3 @@ if RT_USING_LWP
8787
rsource "terminal/Kconfig"
8888
rsource "vdso/Kconfig"
8989
endif
90-

components/lwp/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ if platform in platform_file.keys(): # support platforms
2525
if arch in support_arch.keys() and cpu in support_arch[arch]:
2626
asm_path = 'arch/' + arch + '/' + cpu + '/*_' + platform_file[platform]
2727
arch_common = 'arch/' + arch + '/' + 'common/*.c'
28+
common = 'arch/common/*.c'
2829
if not GetDepend('RT_USING_VDSO'):
2930
vdso_files = ['vdso_data.c', 'vdso.c']
3031
src += [f for f in Glob(arch_common) if os.path.basename(str(f)) not in vdso_files]
32+
src += [f for f in Glob(common) if os.path.basename(str(f)) not in vdso_files]
3133
else:
3234
src += Glob(arch_common)
35+
src += Glob(common)
3336
if not GetDepend('ARCH_MM_MMU'):
3437
excluded_files = ['ioremap.c', 'lwp_futex.c', 'lwp_mm_area.c', 'lwp_pmutex.c', 'lwp_shm.c', 'lwp_user_mm.c']
3538
src += [f for f in Glob('*.c') if os.path.basename(str(f)) not in excluded_files] + Glob(asm_path)

components/lwp/arch/aarch64/common/vdso.c

Lines changed: 0 additions & 110 deletions
This file was deleted.

components/lwp/arch/risc-v/rv64/reloc.c renamed to components/lwp/arch/risc-v/common/reloc.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010

1111
typedef struct
1212
{
13-
Elf64_Word st_name;
14-
Elf64_Addr st_value;
15-
Elf64_Word st_size;
13+
Elf64_Word st_name;
14+
Elf64_Addr st_value;
15+
Elf64_Word st_size;
1616
unsigned char st_info;
1717
unsigned char st_other;
18-
Elf64_Half st_shndx;
18+
Elf64_Half st_shndx;
1919
} Elf64_sym;
2020

2121
#ifdef ARCH_MM_MMU
2222
void arch_elf_reloc(rt_aspace_t aspace, void *text_start, void *rel_dyn_start, size_t rel_dyn_size, void *got_start, size_t got_size, Elf64_sym *dynsym)
2323
{
2424
size_t rel_off;
25-
void* addr;
25+
void *addr;
2626

2727
if (rel_dyn_size && !dynsym)
2828
{
@@ -40,26 +40,26 @@ void arch_elf_reloc(rt_aspace_t aspace, void *text_start, void *rel_dyn_start, s
4040
addr = rt_hw_mmu_v2p(aspace, (void *)((rt_size_t)text_start + v1));
4141
if ((v2 & 0xff) == R_ARM_RELATIVE)
4242
{
43-
*(rt_size_t*)addr += (rt_size_t)text_start;
43+
*(rt_size_t *)addr += (rt_size_t)text_start;
4444
}
4545
else if ((v2 & 0xff) == R_ARM_ABS32)
4646
{
4747
uint32_t t;
4848
t = (v2 >> 8);
4949
if (t) /* 0 is UDF */
5050
{
51-
*(rt_size_t*)addr = (((rt_size_t)text_start) + dynsym[t].st_value);
51+
*(rt_size_t *)addr = (((rt_size_t)text_start) + dynsym[t].st_value);
5252
}
5353
}
5454
}
5555
/* modify got */
5656
if (got_size)
5757
{
58-
uint32_t *got_item = (uint32_t*)got_start;
58+
uint32_t *got_item = (uint32_t *)got_start;
5959

6060
for (rel_off = 0; rel_off < got_size; rel_off += 4, got_item++)
6161
{
62-
addr = rt_hw_mmu_v2p(aspace, got_item);
62+
addr = rt_hw_mmu_v2p(aspace, got_item);
6363
*(rt_size_t *)addr += (rt_size_t)text_start;
6464
}
6565
}
@@ -83,22 +83,22 @@ void arch_elf_reloc(void *text_start, void *rel_dyn_start, size_t rel_dyn_size,
8383

8484
if ((v2 & 0xff) == R_ARM_RELATIVE)
8585
{
86-
*(uint32_t*)(((rt_size_t)text_start) + v1) += (uint32_t)text_start;
86+
*(uint32_t *)(((rt_size_t)text_start) + v1) += (uint32_t)text_start;
8787
}
8888
else if ((v2 & 0xff) == R_ARM_ABS32)
8989
{
9090
uint32_t t;
9191
t = (v2 >> 8);
9292
if (t) /* 0 is UDF */
9393
{
94-
*(uint32_t*)(((rt_size_t)text_start) + v1) = (uint32_t)(((rt_size_t)text_start) + dynsym[t].st_value);
94+
*(uint32_t *)(((rt_size_t)text_start) + v1) = (uint32_t)(((rt_size_t)text_start) + dynsym[t].st_value);
9595
}
9696
}
9797
}
9898
/* modify got */
9999
if (got_size)
100100
{
101-
uint32_t *got_item = (uint32_t*)got_start;
101+
uint32_t *got_item = (uint32_t *)got_start;
102102

103103
for (rel_off = 0; rel_off < got_size; rel_off += 4, got_item++)
104104
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2006-2025 RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-04-22 ScuDays Add VDSO functionality under the riscv64 architecture.
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <ktime.h>
13+
#include <time.h>
14+
#include <vdso_datapage.h>
15+
#include <vdso_data.h>
16+
#include <encoding.h>
17+
18+
void rt_vdso_update_glob_time(void)
19+
{
20+
struct vdso_data *vdata = get_k_vdso_data();
21+
struct timespec *vdso_ts;
22+
uint64_t initdata = vdata->realtime_initdata;
23+
rt_vdso_write_begin(vdata);
24+
25+
vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_REALTIME];
26+
rt_ktime_boottime_get_ns(vdso_ts);
27+
vdso_ts->tv_sec = initdata + vdso_ts->tv_sec;
28+
29+
vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_MONOTONIC];
30+
rt_ktime_boottime_get_ns(vdso_ts);
31+
32+
vdata->cycle_last = rdtime();
33+
rt_vdso_write_end(vdata);
34+
}

components/lwp/terminal/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ menuconfig LWP_USING_TERMINAL
77
if LWP_USING_TERMINAL
88
config LWP_PTY_MAX_PARIS_LIMIT
99
int "Max number of pty devices registered at the same time"
10-
default 64
10+
default 32
1111
help
1212
This upper limit is set to protect kernel memory from draining
1313
out by the application if it keeps allocating pty devices.

components/lwp/vdso/Kconfig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
menuconfig RT_USING_VDSO
2-
bool "vDSO"
1+
config RT_USING_VDSO
2+
bool "vDSO (??Virtual Dynamic Shared Object)"
3+
depends on (ARCH_ARMV8 || ARCH_RISCV)
34
default y
4-
depends on RT_USING_SMART && ARCH_ARMV8

components/lwp/vdso/SConscript

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,51 @@ import os
22
import rtconfig
33
import subprocess
44
from building import *
5-
Import('RTT_ROOT')
65

7-
group = []
8-
9-
cwd = GetCurrentDir()
6+
group = []
7+
cwd = GetCurrentDir()
108
CPPPATH = [cwd, cwd + "/kernel"]
9+
list = os.listdir(cwd)
10+
src = Glob('kernel/*.c') + Glob('kernel/*.S')
1111

1212
if not GetDepend(['RT_USING_VDSO']):
1313
Return('group')
1414

15-
if rtconfig.ARCH != "aarch64":
16-
src = Glob('*.c')
17-
group = DefineGroup('VDSO', src, depend = ['RT_USING_SMART','RT_USING_VDSO'], CPPPATH = CPPPATH)
18-
Return('group')
19-
20-
list = os.listdir(cwd)
21-
src = Glob('kernel/*.c')
22-
src +=Glob('kernel/*.S')
23-
24-
if not os.path.exists(cwd + "/user/vdso.lds"):
25-
Preprocessing("user/vdso.lds.S", ".lds", CPPPATH=[cwd])
26-
27-
#aarch64 vdso xmake
28-
# vdso_file = os.path.join(cwd, 'usr', 'xmake.lua')
29-
# command = ["xmake", "-F", vdso_file]
30-
# clean = ["xmake", "clean"]
31-
32-
vdso_file = os.path.join(cwd, 'user', 'SConstruct')
33-
command = ["scons", "-f", vdso_file]
34-
clean = ["scons", "-f", vdso_file, "--clean"]
35-
36-
if not GetOption('clean'):
37-
result = subprocess.run(command)
15+
if rtconfig.ARCH != "aarch64" and rtconfig.ARCH != "risc-v":
16+
# not supported arch
17+
src = []
3818
else:
39-
result = subprocess.run(clean)
40-
41-
if result.returncode == 0:
42-
print("Command executed successfully")
43-
else:
44-
print("Command failed with exit code:", result.returncode)
45-
exit(1)
46-
47-
group = DefineGroup('VDSO', src, depend = ['RT_USING_SMART','RT_USING_VDSO'], CPPPATH = CPPPATH)
19+
if not hasattr(rtconfig, 'CPP') or rtconfig.CPP is None:
20+
rtconfig.CPP = rtconfig.PREFIX + 'cpp'
21+
if not hasattr(rtconfig, 'CPPFLAGS') or rtconfig.CPPFLAGS is None:
22+
rtconfig.CPPFLAGS = ' -E -P -x assembler-with-cpp'
23+
24+
if not os.path.exists(cwd + "/user" + "/arch" +"/" + rtconfig.ARCH + "/vdso.lds"):
25+
Preprocessing("user/arch/" + rtconfig.ARCH + "/vdso.lds.S", ".lds", CPPPATH=[cwd])
26+
27+
vdso_arch = os.path.join(cwd, 'user',"arch", rtconfig.ARCH)
28+
29+
process_env = os.environ.copy()
30+
if hasattr(rtconfig, 'EXEC_PATH') and rtconfig.EXEC_PATH is not None:
31+
process_env['RTT_EXEC_PATH'] = rtconfig.EXEC_PATH
32+
if hasattr(rtconfig, 'PREFIX') and rtconfig.PREFIX is not None:
33+
process_env['RTT_CC_PREFIX'] = rtconfig.PREFIX
34+
if hasattr(rtconfig, 'DEVICE') and rtconfig.DEVICE is not None:
35+
process_env['RTT_DEVICE'] = rtconfig.DEVICE
36+
37+
command = ["scons", "-C", vdso_arch]
38+
clean_command = ["scons", "-C", vdso_arch, "--clean"]
39+
40+
if not GetOption('clean'):
41+
result = subprocess.run(command, env=process_env, check=True)
42+
else:
43+
result = subprocess.run(clean_command, env=process_env, check=True)
44+
45+
if result.returncode == 0:
46+
print("Command executed successfully")
47+
else:
48+
print("Command failed with exit code:", result.returncode)
49+
exit(1)
50+
51+
group = DefineGroup('vDSO', src, depend = ['RT_USING_SMART','RT_USING_VDSO'], CPPPATH = CPPPATH)
4852
Return('group')

components/lwp/vdso/kernel/vdso_text.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* 2024-07-04 rcitach init ver.
99
*/
1010

11-
#include <vdso_config.h>
11+
#include <vdso_config.h>
1212

1313
.globl __vdso_text_start, __vdso_text_end
1414
.section .rodata

0 commit comments

Comments
 (0)