Skip to content

Commit 87f3248

Browse files
committed
csky: Reconstruct VDSO framework
Reconstruct vdso framework to support future vsyscall, vgettimeofday features. These are very important features to reduce system calls into the kernel for performance improvement. The patch is reference RISC-V's Signed-off-by: Guo Ren <guoren@linux.alibaba.com> Cc: Palmer Dabbelt <palmerdabbelt@google.com>
1 parent e26db7a commit 87f3248

13 files changed

Lines changed: 269 additions & 89 deletions

File tree

‎arch/csky/abiv1/inc/abi/vdso.h‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22

3-
#include <linux/uaccess.h>
3+
#ifndef __ABI_CSKY_VDSO_H
4+
#define __ABI_CSKY_VDSO_H
45

5-
static inline int setup_vdso_page(unsigned short *ptr)
6-
{
7-
int err = 0;
6+
/* movi r1, 127; addi r1, (139 - 127) */
7+
#define SET_SYSCALL_ID .long 0x20b167f1
88

9-
/* movi r1, 127 */
10-
err |= __put_user(0x67f1, ptr + 0);
11-
/* addi r1, (139 - 127) */
12-
err |= __put_user(0x20b1, ptr + 1);
13-
/* trap 0 */
14-
err |= __put_user(0x0008, ptr + 2);
15-
16-
return err;
17-
}
9+
#endif /* __ABI_CSKY_VDSO_H */

‎arch/csky/abiv2/inc/abi/vdso.h‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,7 @@
33
#ifndef __ABI_CSKY_VDSO_H
44
#define __ABI_CSKY_VDSO_H
55

6-
#include <linux/uaccess.h>
6+
/* movi r7, 173 */
7+
#define SET_SYSCALL_ID .long 0x008bea07
78

8-
static inline int setup_vdso_page(unsigned short *ptr)
9-
{
10-
int err = 0;
11-
12-
/* movi r7, 173 */
13-
err |= __put_user(0xea07, ptr);
14-
err |= __put_user(0x008b, ptr+1);
15-
16-
/* trap 0 */
17-
err |= __put_user(0xc000, ptr+2);
18-
err |= __put_user(0x2020, ptr+3);
19-
20-
return err;
21-
}
22-
23-
#endif /* __ABI_CSKY_STRING_H */
9+
#endif /* __ABI_CSKY_VDSO_H */

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@
33
#ifndef __ASM_CSKY_VDSO_H
44
#define __ASM_CSKY_VDSO_H
55

6-
#include <abi/vdso.h>
6+
#include <linux/types.h>
77

8-
struct csky_vdso {
9-
unsigned short rt_signal_retcode[4];
8+
struct vdso_data {
109
};
1110

11+
/*
12+
* The VDSO symbols are mapped into Linux so we can just use regular symbol
13+
* addressing to get their offsets in userspace. The symbols are mapped at an
14+
* offset of 0, but since the linker must support setting weak undefined
15+
* symbols to the absolute address 0 it also happens to support other low
16+
* addresses even when the code model suggests those low addresses would not
17+
* otherwise be availiable.
18+
*/
19+
#define VDSO_SYMBOL(base, name) \
20+
({ \
21+
extern const char __vdso_##name[]; \
22+
(void __user *)((unsigned long)(base) + __vdso_##name); \
23+
})
24+
1225
#endif /* __ASM_CSKY_VDSO_H */

‎arch/csky/kernel/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
extra-y := head.o vmlinux.lds
33

4-
obj-y += entry.o atomic.o signal.o traps.o irq.o time.o vdso.o
4+
obj-y += entry.o atomic.o signal.o traps.o irq.o time.o vdso.o vdso/
55
obj-y += power.o syscall.o syscall_table.o setup.o
66
obj-y += process.o cpu-probe.o ptrace.o stacktrace.o
77
obj-y += probes/

‎arch/csky/kernel/signal.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
134134
{
135135
struct rt_sigframe *frame;
136136
int err = 0;
137-
struct csky_vdso *vdso = current->mm->context.vdso;
138137

139138
frame = get_sigframe(ksig, regs, sizeof(*frame));
140139
if (!access_ok(frame, sizeof(*frame)))
@@ -152,7 +151,8 @@ setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
152151
return -EFAULT;
153152

154153
/* Set up to return from userspace. */
155-
regs->lr = (unsigned long)(vdso->rt_signal_retcode);
154+
regs->lr = (unsigned long)VDSO_SYMBOL(
155+
current->mm->context.vdso, rt_sigreturn);
156156

157157
/*
158158
* Set up registers for signal handler.

‎arch/csky/kernel/vdso.c‎

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,103 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
33

4-
#include <linux/kernel.h>
5-
#include <linux/err.h>
6-
#include <linux/sched.h>
7-
#include <linux/mm.h>
8-
#include <linux/init.h>
94
#include <linux/binfmts.h>
105
#include <linux/elf.h>
11-
#include <linux/vmalloc.h>
12-
#include <linux/unistd.h>
13-
#include <linux/uaccess.h>
6+
#include <linux/err.h>
7+
#include <linux/mm.h>
8+
#include <linux/slab.h>
149

10+
#include <asm/page.h>
1511
#include <asm/vdso.h>
16-
#include <asm/cacheflush.h>
1712

18-
static struct page *vdso_page;
13+
extern char vdso_start[], vdso_end[];
1914

20-
static int __init init_vdso(void)
21-
{
22-
struct csky_vdso *vdso;
23-
int err = 0;
24-
25-
vdso_page = alloc_page(GFP_KERNEL);
26-
if (!vdso_page)
27-
panic("Cannot allocate vdso");
15+
static unsigned int vdso_pages;
16+
static struct page **vdso_pagelist;
2817

29-
vdso = vmap(&vdso_page, 1, 0, PAGE_KERNEL);
30-
if (!vdso)
31-
panic("Cannot map vdso");
18+
/*
19+
* The vDSO data page.
20+
*/
21+
static union {
22+
struct vdso_data data;
23+
u8 page[PAGE_SIZE];
24+
} vdso_data_store __page_aligned_data;
25+
struct vdso_data *vdso_data = &vdso_data_store.data;
3226

33-
clear_page(vdso);
34-
35-
err = setup_vdso_page(vdso->rt_signal_retcode);
36-
if (err)
37-
panic("Cannot set signal return code, err: %x.", err);
27+
static int __init vdso_init(void)
28+
{
29+
unsigned int i;
30+
31+
vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
32+
vdso_pagelist =
33+
kcalloc(vdso_pages + 1, sizeof(struct page *), GFP_KERNEL);
34+
if (unlikely(vdso_pagelist == NULL)) {
35+
pr_err("vdso: pagelist allocation failed\n");
36+
return -ENOMEM;
37+
}
3838

39-
dcache_wb_range((unsigned long)vdso, (unsigned long)vdso + 16);
39+
for (i = 0; i < vdso_pages; i++) {
40+
struct page *pg;
4041

41-
vunmap(vdso);
42+
pg = virt_to_page(vdso_start + (i << PAGE_SHIFT));
43+
vdso_pagelist[i] = pg;
44+
}
45+
vdso_pagelist[i] = virt_to_page(vdso_data);
4246

4347
return 0;
4448
}
45-
subsys_initcall(init_vdso);
49+
arch_initcall(vdso_init);
4650

47-
int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
51+
int arch_setup_additional_pages(struct linux_binprm *bprm,
52+
int uses_interp)
4853
{
49-
int ret;
50-
unsigned long addr;
5154
struct mm_struct *mm = current->mm;
55+
unsigned long vdso_base, vdso_len;
56+
int ret;
5257

53-
mmap_write_lock(mm);
58+
vdso_len = (vdso_pages + 1) << PAGE_SHIFT;
5459

55-
addr = get_unmapped_area(NULL, STACK_TOP, PAGE_SIZE, 0, 0);
56-
if (IS_ERR_VALUE(addr)) {
57-
ret = addr;
58-
goto up_fail;
60+
mmap_write_lock(mm);
61+
vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
62+
if (IS_ERR_VALUE(vdso_base)) {
63+
ret = vdso_base;
64+
goto end;
5965
}
6066

61-
ret = install_special_mapping(
62-
mm,
63-
addr,
64-
PAGE_SIZE,
65-
VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
66-
&vdso_page);
67-
if (ret)
68-
goto up_fail;
67+
/*
68+
* Put vDSO base into mm struct. We need to do this before calling
69+
* install_special_mapping or the perf counter mmap tracking code
70+
* will fail to recognise it as a vDSO (since arch_vma_name fails).
71+
*/
72+
mm->context.vdso = (void *)vdso_base;
73+
74+
ret =
75+
install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
76+
(VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
77+
vdso_pagelist);
78+
79+
if (unlikely(ret)) {
80+
mm->context.vdso = NULL;
81+
goto end;
82+
}
6983

70-
mm->context.vdso = (void *)addr;
84+
vdso_base += (vdso_pages << PAGE_SHIFT);
85+
ret = install_special_mapping(mm, vdso_base, PAGE_SIZE,
86+
(VM_READ | VM_MAYREAD), &vdso_pagelist[vdso_pages]);
7187

72-
up_fail:
88+
if (unlikely(ret))
89+
mm->context.vdso = NULL;
90+
end:
7391
mmap_write_unlock(mm);
7492
return ret;
7593
}
7694

7795
const char *arch_vma_name(struct vm_area_struct *vma)
7896
{
79-
if (vma->vm_mm == NULL)
80-
return NULL;
81-
82-
if (vma->vm_start == (long)vma->vm_mm->context.vdso)
97+
if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso))
8398
return "[vdso]";
84-
else
85-
return NULL;
99+
if (vma->vm_mm && (vma->vm_start ==
100+
(long)vma->vm_mm->context.vdso + PAGE_SIZE))
101+
return "[vdso_data]";
102+
return NULL;
86103
}

‎arch/csky/kernel/vdso/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
vdso.lds
3+
*.tmp
4+
vdso-syms.S

‎arch/csky/kernel/vdso/Makefile‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
3+
# Absolute relocation type $(ARCH_REL_TYPE_ABS) needs to be defined before
4+
# the inclusion of generic Makefile.
5+
ARCH_REL_TYPE_ABS := R_CKCORE_ADDR32|R_CKCORE_JUMP_SLOT
6+
include $(srctree)/lib/vdso/Makefile
7+
8+
# Symbols present in the vdso
9+
vdso-syms = rt_sigreturn
10+
11+
# Files to link into the vdso
12+
obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o
13+
14+
ccflags-y := -fno-stack-protector
15+
16+
# Build rules
17+
targets := $(obj-vdso) vdso.so vdso.so.dbg vdso.lds vdso-dummy.o
18+
obj-vdso := $(addprefix $(obj)/, $(obj-vdso))
19+
20+
obj-y += vdso.o vdso-syms.o
21+
CPPFLAGS_vdso.lds += -P -C -U$(ARCH)
22+
23+
# Disable gcov profiling for VDSO code
24+
GCOV_PROFILE := n
25+
KCOV_INSTRUMENT := n
26+
27+
# Force dependency
28+
$(obj)/vdso.o: $(obj)/vdso.so
29+
30+
SYSCFLAGS_vdso.so.dbg = $(c_flags)
31+
$(obj)/vdso.so.dbg: $(src)/vdso.lds $(obj-vdso) FORCE
32+
$(call if_changed,vdsold)
33+
SYSCFLAGS_vdso.so.dbg = -shared -s -Wl,-soname=linux-vdso.so.1 \
34+
-Wl,--build-id=sha1 -Wl,--hash-style=both
35+
36+
$(obj)/vdso-syms.S: $(obj)/vdso.so FORCE
37+
$(call if_changed,so2s)
38+
39+
# strip rule for the .so file
40+
$(obj)/%.so: OBJCOPYFLAGS := -S
41+
$(obj)/%.so: $(obj)/%.so.dbg FORCE
42+
$(call if_changed,objcopy)
43+
44+
# actual build commands
45+
# The DSO images are built using a special linker script
46+
# Make sure only to export the intended __vdso_xxx symbol offsets.
47+
quiet_cmd_vdsold = VDSOLD $@
48+
cmd_vdsold = $(CC) $(KBUILD_CFLAGS) $(call cc-option, -no-pie) -nostdlib -nostartfiles $(SYSCFLAGS_$(@F)) \
49+
-Wl,-T,$(filter-out FORCE,$^) -o $@.tmp && \
50+
$(CROSS_COMPILE)objcopy \
51+
$(patsubst %, -G __vdso_%, $(vdso-syms)) $@.tmp $@ && \
52+
rm $@.tmp
53+
54+
# Extracts symbol offsets from the VDSO, converting them into an assembly file
55+
# that contains the same symbols at the same offsets.
56+
quiet_cmd_so2s = SO2S $@
57+
cmd_so2s = $(NM) -D $< | $(srctree)/$(src)/so2s.sh > $@
58+
59+
# install commands for the unstripped file
60+
quiet_cmd_vdso_install = INSTALL $@
61+
cmd_vdso_install = cp $(obj)/$@.dbg $(MODLIB)/vdso/$@
62+
63+
vdso.so: $(obj)/vdso.so.dbg
64+
@mkdir -p $(MODLIB)/vdso
65+
$(call cmd,vdso_install)
66+
67+
vdso_install: vdso.so

‎arch/csky/kernel/vdso/note.S‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* This supplies .note.* sections to go into the PT_NOTE inside the vDSO text.
4+
* Here we can supply some information useful to userland.
5+
*/
6+
7+
#include <linux/elfnote.h>
8+
#include <linux/version.h>
9+
10+
ELFNOTE_START(Linux, 0, "a")
11+
.long LINUX_VERSION_CODE
12+
ELFNOTE_END
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-only */
2+
3+
#include <linux/linkage.h>
4+
#include <asm/unistd.h>
5+
#include <abi/vdso.h>
6+
7+
.text
8+
ENTRY(__vdso_rt_sigreturn)
9+
.cfi_startproc
10+
.cfi_signal_frame
11+
SET_SYSCALL_ID
12+
trap 0
13+
.cfi_endproc
14+
ENDPROC(__vdso_rt_sigreturn)

0 commit comments

Comments
 (0)