Skip to content

Commit eefacd1

Browse files
author
Greentime Hu
committed
nds32: VDSO support
This patch adds VDSO support. The VDSO code is currently used for sys_rt_sigreturn() and optimised gettimeofday() (using the SoC timer counter). Signed-off-by: Vincent Chen <vincentc@andestech.com> Signed-off-by: Greentime Hu <greentime@andestech.com> Acked-by: Arnd Bergmann <arnd@arndb.de>
1 parent 1932fbe commit eefacd1

12 files changed

Lines changed: 816 additions & 0 deletions

File tree

‎arch/nds32/include/asm/vdso.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-2.0
3+
* Copyright (C) 2005-2017 Andes Technology Corporation
4+
*/
5+
6+
#ifndef __ASM_VDSO_H
7+
#define __ASM_VDSO_H
8+
9+
#ifdef __KERNEL__
10+
11+
#ifndef __ASSEMBLY__
12+
13+
#include <generated/vdso-offsets.h>
14+
15+
#define VDSO_SYMBOL(base, name) \
16+
({ \
17+
(unsigned long)(vdso_offset_##name + (unsigned long)(base)); \
18+
})
19+
20+
#endif /* !__ASSEMBLY__ */
21+
22+
#endif /* __KERNEL__ */
23+
24+
#endif /* __ASM_VDSO_H */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2012 ARM Limited
3+
// Copyright (C) 2005-2017 Andes Technology Corporation
4+
#ifndef __ASM_VDSO_DATAPAGE_H
5+
#define __ASM_VDSO_DATAPAGE_H
6+
7+
#ifdef __KERNEL__
8+
9+
#ifndef __ASSEMBLY__
10+
11+
struct vdso_data {
12+
bool cycle_count_down; /* timer cyclye counter is decrease with time */
13+
u32 cycle_count_offset; /* offset of timer cycle counter register */
14+
u32 seq_count; /* sequence count - odd during updates */
15+
u32 xtime_coarse_sec; /* coarse time */
16+
u32 xtime_coarse_nsec;
17+
18+
u32 wtm_clock_sec; /* wall to monotonic offset */
19+
u32 wtm_clock_nsec;
20+
u32 xtime_clock_sec; /* CLOCK_REALTIME - seconds */
21+
u32 cs_mult; /* clocksource multiplier */
22+
u32 cs_shift; /* Cycle to nanosecond divisor (power of two) */
23+
24+
u64 cs_cycle_last; /* last cycle value */
25+
u64 cs_mask; /* clocksource mask */
26+
27+
u64 xtime_clock_nsec; /* CLOCK_REALTIME sub-ns base */
28+
u32 tz_minuteswest; /* timezone info for gettimeofday(2) */
29+
u32 tz_dsttime;
30+
};
31+
32+
#endif /* !__ASSEMBLY__ */
33+
34+
#endif /* __KERNEL__ */
35+
36+
#endif /* __ASM_VDSO_DATAPAGE_H */
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2005-2017 Andes Technology Corporation
3+
4+
extern struct timer_info_t timer_info;
5+
#define EMPTY_VALUE ~(0UL)
6+
#define EMPTY_TIMER_MAPPING EMPTY_VALUE
7+
#define EMPTY_REG_OFFSET EMPTY_VALUE
8+
9+
struct timer_info_t
10+
{
11+
bool cycle_count_down;
12+
unsigned long mapping_base;
13+
unsigned long cycle_count_reg_offset;
14+
};

‎arch/nds32/kernel/vdso.c‎

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2012 ARM Limited
3+
// Copyright (C) 2005-2017 Andes Technology Corporation
4+
5+
#include <linux/cache.h>
6+
#include <linux/clocksource.h>
7+
#include <linux/elf.h>
8+
#include <linux/err.h>
9+
#include <linux/errno.h>
10+
#include <linux/gfp.h>
11+
#include <linux/kernel.h>
12+
#include <linux/mm.h>
13+
#include <linux/sched.h>
14+
#include <linux/signal.h>
15+
#include <linux/slab.h>
16+
#include <linux/timekeeper_internal.h>
17+
#include <linux/vmalloc.h>
18+
#include <linux/random.h>
19+
20+
#include <asm/cacheflush.h>
21+
#include <asm/vdso.h>
22+
#include <asm/vdso_datapage.h>
23+
#include <asm/vdso_timer_info.h>
24+
#include <asm/cache_info.h>
25+
extern struct cache_info L1_cache_info[2];
26+
extern char vdso_start, vdso_end;
27+
static unsigned long vdso_pages __ro_after_init;
28+
static unsigned long timer_mapping_base;
29+
30+
struct timer_info_t timer_info = {
31+
.cycle_count_down = true,
32+
.mapping_base = EMPTY_TIMER_MAPPING,
33+
.cycle_count_reg_offset = EMPTY_REG_OFFSET
34+
};
35+
/*
36+
* The vDSO data page.
37+
*/
38+
static struct page *no_pages[] = { NULL };
39+
40+
static union {
41+
struct vdso_data data;
42+
u8 page[PAGE_SIZE];
43+
} vdso_data_store __page_aligned_data;
44+
struct vdso_data *vdso_data = &vdso_data_store.data;
45+
static struct vm_special_mapping vdso_spec[2] __ro_after_init = {
46+
{
47+
.name = "[vvar]",
48+
.pages = no_pages,
49+
},
50+
{
51+
.name = "[vdso]",
52+
},
53+
};
54+
55+
static void get_timer_node_info(void)
56+
{
57+
timer_mapping_base = timer_info.mapping_base;
58+
vdso_data->cycle_count_offset =
59+
timer_info.cycle_count_reg_offset;
60+
vdso_data->cycle_count_down =
61+
timer_info.cycle_count_down;
62+
}
63+
64+
static int __init vdso_init(void)
65+
{
66+
int i;
67+
struct page **vdso_pagelist;
68+
69+
if (memcmp(&vdso_start, "\177ELF", 4)) {
70+
pr_err("vDSO is not a valid ELF object!\n");
71+
return -EINVAL;
72+
}
73+
/* Creat a timer io mapping to get clock cycles counter */
74+
get_timer_node_info();
75+
76+
vdso_pages = (&vdso_end - &vdso_start) >> PAGE_SHIFT;
77+
pr_info("vdso: %ld pages (%ld code @ %p, %ld data @ %p)\n",
78+
vdso_pages + 1, vdso_pages, &vdso_start, 1L, vdso_data);
79+
80+
/* Allocate the vDSO pagelist */
81+
vdso_pagelist = kcalloc(vdso_pages, sizeof(struct page *), GFP_KERNEL);
82+
if (vdso_pagelist == NULL)
83+
return -ENOMEM;
84+
85+
for (i = 0; i < vdso_pages; i++)
86+
vdso_pagelist[i] = virt_to_page(&vdso_start + i * PAGE_SIZE);
87+
vdso_spec[1].pages = &vdso_pagelist[0];
88+
89+
return 0;
90+
}
91+
92+
arch_initcall(vdso_init);
93+
94+
unsigned long inline vdso_random_addr(unsigned long vdso_mapping_len)
95+
{
96+
unsigned long start = current->mm->mmap_base, end, offset, addr;
97+
start = PAGE_ALIGN(start);
98+
99+
/* Round the lowest possible end address up to a PMD boundary. */
100+
end = (start + vdso_mapping_len + PMD_SIZE - 1) & PMD_MASK;
101+
if (end >= TASK_SIZE)
102+
end = TASK_SIZE;
103+
end -= vdso_mapping_len;
104+
105+
if (end > start) {
106+
offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
107+
addr = start + (offset << PAGE_SHIFT);
108+
} else {
109+
addr = start;
110+
}
111+
return addr;
112+
}
113+
114+
int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
115+
{
116+
struct mm_struct *mm = current->mm;
117+
unsigned long vdso_base, vdso_text_len, vdso_mapping_len;
118+
struct vm_area_struct *vma;
119+
unsigned long addr = 0;
120+
pgprot_t prot;
121+
int ret, vvar_page_num = 2;
122+
123+
vdso_text_len = vdso_pages << PAGE_SHIFT;
124+
125+
if(timer_mapping_base == EMPTY_VALUE)
126+
vvar_page_num = 1;
127+
/* Be sure to map the data page */
128+
vdso_mapping_len = vdso_text_len + vvar_page_num * PAGE_SIZE;
129+
#ifdef CONFIG_CPU_CACHE_ALIASING
130+
vdso_mapping_len += L1_cache_info[DCACHE].aliasing_num - 1;
131+
#endif
132+
133+
if (down_write_killable(&mm->mmap_sem))
134+
return -EINTR;
135+
136+
addr = vdso_random_addr(vdso_mapping_len);
137+
vdso_base = get_unmapped_area(NULL, addr, vdso_mapping_len, 0, 0);
138+
if (IS_ERR_VALUE(vdso_base)) {
139+
ret = vdso_base;
140+
goto up_fail;
141+
}
142+
143+
#ifdef CONFIG_CPU_CACHE_ALIASING
144+
{
145+
unsigned int aliasing_mask =
146+
L1_cache_info[DCACHE].aliasing_mask;
147+
unsigned int page_colour_ofs;
148+
page_colour_ofs = ((unsigned int)vdso_data & aliasing_mask) -
149+
(vdso_base & aliasing_mask);
150+
vdso_base += page_colour_ofs & aliasing_mask;
151+
}
152+
#endif
153+
154+
vma = _install_special_mapping(mm, vdso_base, vvar_page_num * PAGE_SIZE,
155+
VM_READ | VM_MAYREAD, &vdso_spec[0]);
156+
if (IS_ERR(vma)) {
157+
ret = PTR_ERR(vma);
158+
goto up_fail;
159+
}
160+
161+
/*Map vdata to user space */
162+
ret = io_remap_pfn_range(vma, vdso_base,
163+
virt_to_phys(vdso_data) >> PAGE_SHIFT,
164+
PAGE_SIZE, vma->vm_page_prot);
165+
if (ret)
166+
goto up_fail;
167+
168+
/*Map timer to user space */
169+
vdso_base += PAGE_SIZE;
170+
prot = __pgprot(_PAGE_V | _PAGE_M_UR_KR | _PAGE_D | _PAGE_C_DEV);
171+
ret = io_remap_pfn_range(vma, vdso_base, timer_mapping_base >> PAGE_SHIFT,
172+
PAGE_SIZE, prot);
173+
if (ret)
174+
goto up_fail;
175+
176+
/*Map vdso to user space */
177+
vdso_base += PAGE_SIZE;
178+
mm->context.vdso = (void *)vdso_base;
179+
vma = _install_special_mapping(mm, vdso_base, vdso_text_len,
180+
VM_READ | VM_EXEC |
181+
VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
182+
&vdso_spec[1]);
183+
if (IS_ERR(vma)) {
184+
ret = PTR_ERR(vma);
185+
goto up_fail;
186+
}
187+
188+
up_write(&mm->mmap_sem);
189+
return 0;
190+
191+
up_fail:
192+
mm->context.vdso = NULL;
193+
up_write(&mm->mmap_sem);
194+
return ret;
195+
}
196+
197+
static void vdso_write_begin(struct vdso_data *vdata)
198+
{
199+
++vdso_data->seq_count;
200+
smp_wmb(); /* Pairs with smp_rmb in vdso_read_retry */
201+
}
202+
203+
static void vdso_write_end(struct vdso_data *vdata)
204+
{
205+
smp_wmb(); /* Pairs with smp_rmb in vdso_read_begin */
206+
++vdso_data->seq_count;
207+
}
208+
209+
void update_vsyscall(struct timekeeper *tk)
210+
{
211+
vdso_write_begin(vdso_data);
212+
vdso_data->cs_mask = tk->tkr_mono.mask;
213+
vdso_data->cs_mult = tk->tkr_mono.mult;
214+
vdso_data->cs_shift = tk->tkr_mono.shift;
215+
vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
216+
vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec;
217+
vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;
218+
vdso_data->xtime_clock_sec = tk->xtime_sec;
219+
vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
220+
vdso_data->xtime_coarse_sec = tk->xtime_sec;
221+
vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >>
222+
tk->tkr_mono.shift;
223+
vdso_write_end(vdso_data);
224+
}
225+
226+
void update_vsyscall_tz(void)
227+
{
228+
vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
229+
vdso_data->tz_dsttime = sys_tz.tz_dsttime;
230+
}

‎arch/nds32/kernel/vdso/Makefile‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#
2+
# Building a vDSO image for AArch64.
3+
#
4+
# Author: Will Deacon <will.deacon@arm.com>
5+
# Heavily based on the vDSO Makefiles for other archs.
6+
#
7+
8+
obj-vdso := note.o datapage.o sigreturn.o gettimeofday.o
9+
10+
# Build rules
11+
targets := $(obj-vdso) vdso.so vdso.so.dbg
12+
obj-vdso := $(addprefix $(obj)/, $(obj-vdso))
13+
14+
ccflags-y := -shared -fno-common -fno-builtin
15+
ccflags-y += -nostdlib -Wl,-soname=linux-vdso.so.1 \
16+
$(call cc-ldoption, -Wl$(comma)--hash-style=sysv)
17+
ccflags-y += -fPIC -Wl,-shared -g
18+
19+
# Disable gcov profiling for VDSO code
20+
GCOV_PROFILE := n
21+
22+
23+
obj-y += vdso.o
24+
extra-y += vdso.lds
25+
CPPFLAGS_vdso.lds += -P -C -U$(ARCH)
26+
27+
# Force dependency
28+
$(obj)/vdso.o : $(obj)/vdso.so
29+
30+
# Link rule for the .so file, .lds has to be first
31+
$(obj)/vdso.so.dbg: $(src)/vdso.lds $(obj-vdso)
32+
$(call if_changed,vdsold)
33+
34+
35+
# Strip rule for the .so file
36+
$(obj)/%.so: OBJCOPYFLAGS := -S
37+
$(obj)/%.so: $(obj)/%.so.dbg FORCE
38+
$(call if_changed,objcopy)
39+
40+
# Generate VDSO offsets using helper script
41+
gen-vdsosym := $(srctree)/$(src)/gen_vdso_offsets.sh
42+
quiet_cmd_vdsosym = VDSOSYM $@
43+
define cmd_vdsosym
44+
$(NM) $< | $(gen-vdsosym) | LC_ALL=C sort > $@
45+
endef
46+
47+
include/generated/vdso-offsets.h: $(obj)/vdso.so.dbg FORCE
48+
$(call if_changed,vdsosym)
49+
50+
51+
52+
# Assembly rules for the .S files
53+
54+
sigreturn.o : sigreturn.S
55+
$(call if_changed_dep,vdsoas)
56+
57+
note.o : note.S
58+
$(call if_changed_dep,vdsoas)
59+
60+
datapage.o : datapage.S
61+
$(call if_changed_dep,vdsoas)
62+
63+
gettimeofday.o : gettimeofday.c FORCE
64+
$(call if_changed_dep,vdsocc)
65+
66+
# Actual build commands
67+
quiet_cmd_vdsold = VDSOL $@
68+
cmd_vdsold = $(CC) $(c_flags) -Wl,-n -Wl,-T $^ -o $@
69+
quiet_cmd_vdsoas = VDSOA $@
70+
cmd_vdsoas = $(CC) $(a_flags) -c -o $@ $<
71+
quiet_cmd_vdsocc = VDSOA $@
72+
cmd_vdsocc = $(CC) $(c_flags) -c -o $@ $<
73+
74+
# Install commands for the unstripped file
75+
quiet_cmd_vdso_install = INSTALL $@
76+
cmd_vdso_install = cp $(obj)/$@.dbg $(MODLIB)/vdso/$@
77+
78+
vdso.so: $(obj)/vdso.so.dbg
79+
@mkdir -p $(MODLIB)/vdso
80+
$(call cmd,vdso_install)
81+
82+
vdso_install: vdso.so

0 commit comments

Comments
 (0)