Skip to content

Commit 7f0131c

Browse files
committed
Add VDSO functionality under the riscv64 architecture.
1 parent e7d870b commit 7f0131c

File tree

15 files changed

+444
-112
lines changed

15 files changed

+444
-112
lines changed

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 renamed to components/lwp/arch/common/vdso.c

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,82 @@
11
/*
2-
* Copyright (c) 2006-2024 RT-Thread Development Team
2+
* Copyright (c) 2006-2025 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
77
* Date Author Notes
8-
* 2024-07-04 rcitach init ver.
8+
* 2025-04-22 ScuDays Add VDSO functionality under the riscv64 architecture.
99
*/
1010

1111
#include <rtthread.h>
1212
#include <mmu.h>
13-
#include <gtimer.h>
1413
#include <lwp_user_mm.h>
1514

1615
#include "vdso.h"
1716
#include "vdso_datapage.h"
18-
#define DBG_TAG "vdso"
19-
#define DBG_LVL DBG_INFO
17+
#define DBG_TAG "vdso"
18+
#define DBG_LVL DBG_INFO
2019
#include <rtdbg.h>
2120

22-
enum vdso_abi {
23-
VDSO_ABI_AA64,
21+
enum vdso_abi
22+
{
23+
VDSO_ABI_COMMON,
2424
};
25-
enum vvar_pages {
25+
26+
enum vvar_pages
27+
{
2628
VVAR_DATA_PAGE_OFFSET,
2729
VVAR_TIMENS_PAGE_OFFSET,
2830
VVAR_NR_PAGES,
2931
};
30-
struct vdso_abi_info {
31-
const char *name;
32-
const char *vdso_code_start;
33-
const char *vdso_code_end;
34-
unsigned long vdso_pages;
3532

33+
34+
struct vdso_abi_info
35+
{
36+
const char *name;
37+
const char *vdso_code_start;
38+
const char *vdso_code_end;
39+
unsigned long vdso_pages;
3640
};
3741

3842
static struct vdso_abi_info vdso_info[] = {
39-
[VDSO_ABI_AA64] = {
40-
.name = "vdso_aarch64",
41-
.vdso_code_start = __vdso_text_start,
42-
.vdso_code_end = __vdso_text_end,
43-
},
43+
[VDSO_ABI_COMMON] = {
44+
.name = "vdso_common",
45+
.vdso_code_start = __vdso_text_start,
46+
.vdso_code_end = __vdso_text_end,
47+
},
4448
};
4549

4650
static union {
47-
struct vdso_data data[CS_BASES];
48-
uint8_t page[ARCH_PAGE_SIZE];
51+
struct vdso_data data[CS_BASES];
52+
uint8_t page[ARCH_PAGE_SIZE];
4953
} vdso_data_store __page_aligned_data;
50-
struct vdso_data *vdso_data = vdso_data_store.data;
51-
int init_ret_flag = RT_EOK;
54+
struct vdso_data *vdso_data = vdso_data_store.data;
55+
int init_ret_flag = RT_EOK;
5256

5357
static int __setup_additional_pages(enum vdso_abi abi, struct rt_lwp *lwp)
5458
{
5559
RT_ASSERT(lwp != RT_NULL);
5660

57-
int ret;
58-
void *vdso_base = RT_NULL;
61+
int ret;
62+
void *vdso_base = RT_NULL;
5963
unsigned long vdso_data_len, vdso_text_len;
6064

6165
vdso_data_len = VVAR_NR_PAGES * ARCH_PAGE_SIZE;
6266
vdso_text_len = vdso_info[abi].vdso_pages << ARCH_PAGE_SHIFT;
6367

6468
vdso_base = lwp_map_user_phy(lwp, RT_NULL, rt_kmem_v2p((void *)vdso_data), vdso_data_len, 0);
65-
if(vdso_base != RT_NULL)
69+
if (vdso_base != RT_NULL)
6670
{
6771
ret = RT_EOK;
6872
}
6973
else
7074
{
7175
ret = RT_ERROR;
7276
}
77+
7378
vdso_base += vdso_data_len;
74-
vdso_base = lwp_map_user_phy(lwp, vdso_base, rt_kmem_v2p((void *)vdso_info[abi].vdso_code_start), vdso_text_len, 0);
79+
vdso_base = lwp_map_user_phy(lwp, vdso_base, rt_kmem_v2p((void *)vdso_info[abi].vdso_code_start), vdso_text_len, 0);
7580

7681
lwp->vdso_vbase = vdso_base;
7782
return ret;
@@ -81,28 +86,28 @@ int arch_setup_additional_pages(struct rt_lwp *lwp)
8186
{
8287
int ret;
8388
if (init_ret_flag != RT_EOK) return -RT_ERROR;
84-
ret = __setup_additional_pages(VDSO_ABI_AA64, lwp);
89+
ret = __setup_additional_pages(VDSO_ABI_COMMON, lwp);
8590

8691
return ret;
8792
}
8893

94+
8995
static void __initdata(void)
9096
{
91-
struct tm time_vdso = SOFT_RTC_VDSOTIME_DEFAULT;
97+
struct tm time_vdso = SOFT_RTC_VDSOTIME_DEFAULT;
9298
vdso_data->realtime_initdata = timegm(&time_vdso);
9399
}
94100

101+
95102
static int validate_vdso_elf(void)
96103
{
97-
if (rt_memcmp(vdso_info[VDSO_ABI_AA64].vdso_code_start, ELF_HEAD, ELF_HEAD_LEN)) {
104+
if (rt_memcmp(vdso_info[VDSO_ABI_COMMON].vdso_code_start, ELF_HEAD, ELF_HEAD_LEN))
105+
{
98106
LOG_E("vDSO is not a valid ELF object!");
99107
init_ret_flag = -RT_ERROR;
100108
return -RT_ERROR;
101109
}
102-
vdso_info[VDSO_ABI_AA64].vdso_pages = (
103-
vdso_info[VDSO_ABI_AA64].vdso_code_end -
104-
vdso_info[VDSO_ABI_AA64].vdso_code_start) >>
105-
ARCH_PAGE_SHIFT;
110+
vdso_info[VDSO_ABI_COMMON].vdso_pages = (vdso_info[VDSO_ABI_COMMON].vdso_code_end - vdso_info[VDSO_ABI_COMMON].vdso_code_start) >> ARCH_PAGE_SHIFT;
106111

107112
__initdata();
108113
return RT_EOK;

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/vdso/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
menuconfig RT_USING_VDSO
22
bool "vDSO"
33
default y
4-
depends on RT_USING_SMART && ARCH_ARMV8
4+
depends on RT_USING_SMART && (ARCH_ARMV8 || ARCH_RISCV)

components/lwp/vdso/SConscript

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ CPPPATH = [cwd, cwd + "/kernel"]
1212
if not GetDepend(['RT_USING_VDSO']):
1313
Return('group')
1414

15-
if rtconfig.ARCH != "aarch64":
15+
if rtconfig.ARCH != "aarch64" and rtconfig.ARCH != "risc-v":
1616
src = Glob('*.c')
1717
group = DefineGroup('VDSO', src, depend = ['RT_USING_SMART','RT_USING_VDSO'], CPPPATH = CPPPATH)
1818
Return('group')
@@ -21,15 +21,15 @@ list = os.listdir(cwd)
2121
src = Glob('kernel/*.c')
2222
src +=Glob('kernel/*.S')
2323

24-
if not os.path.exists(cwd + "/user/vdso.lds"):
25-
Preprocessing("user/vdso.lds.S", ".lds", CPPPATH=[cwd])
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])
2626

2727
#aarch64 vdso xmake
2828
# vdso_file = os.path.join(cwd, 'usr', 'xmake.lua')
2929
# command = ["xmake", "-F", vdso_file]
3030
# clean = ["xmake", "clean"]
3131

32-
vdso_file = os.path.join(cwd, 'user', 'SConstruct')
32+
vdso_file = os.path.join(cwd, 'user',"arch", rtconfig.ARCH, 'SConstruct')
3333
command = ["scons", "-f", vdso_file]
3434
clean = ["scons", "-f", vdso_file, "--clean"]
3535

components/lwp/vdso/user/SConstruct renamed to components/lwp/vdso/user/arch/aarch64/SConstruct

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import subprocess
44

55
arguments = sys.argv[2]
66
vdso_usr = os.path.dirname(arguments)
7-
vdso_root = os.path.dirname(vdso_usr)
7+
user_path = os.path.dirname(vdso_usr)
8+
user_path = os.path.dirname(user_path)
9+
user_path = os.path.dirname(user_path)
810

911

1012
EXEC_PATH = os.getenv('RTT_EXEC_PATH') or '/usr/bin'
@@ -22,8 +24,7 @@ CXXFLAGS = DEVICE + ' -Wall -fdiagnostics-color=always'
2224
AFLAGS = ' -x assembler-with-cpp'
2325
CFLAGS = DEVICE + ' -Wall -Wno-cpp -std=gnu99 -fdiagnostics-color=always -fPIC -O2'
2426
LFLAGS = DEVICE + ' -Bsymbolic -Wl,--gc-sections,-u,system_vectors -T {path}/vdso.lds'.format(path=vdso_usr)
25-
CFLAGS += " -I {path} -I{path}/user".format(path=vdso_root)
26-
27+
CFLAGS += " -I {path} -I {upath} ".format(path=vdso_usr ,upath=user_path)
2728
env = Environment(tools=['gcc', 'link'],
2829
AS = AS, ASFLAGS = AFLAGS,
2930
CC = CC, CFLAGS = CFLAGS,
@@ -34,6 +35,6 @@ env.PrependENVPath('PATH', EXEC_PATH)
3435

3536
src = os.path.join(vdso_usr,'vdso_sys.c')
3637
target_name = 'librtos_vdso.so'
37-
target = os.path.join(vdso_usr, "build", target_name)
38+
target = os.path.join(user_path + "/user", "build", target_name)
3839
shared_lib = env.SharedLibrary(target=target, source=src)
3940
env.Default(shared_lib)

0 commit comments

Comments
 (0)