Skip to content

Commit e2c0cdf

Browse files
RISC-V: User-facing API
This patch contains code that is in some way visible to the user: including via system calls, the VDSO, module loading and signal handling. It also contains some generic code that is ABI visible. Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 07037db commit e2c0cdf

27 files changed

+1687
-0
lines changed

arch/riscv/include/asm/mmu.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2012 Regents of the University of California
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, version 2.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*/
13+
14+
15+
#ifndef _ASM_RISCV_MMU_H
16+
#define _ASM_RISCV_MMU_H
17+
18+
#ifndef __ASSEMBLY__
19+
20+
typedef struct {
21+
void *vdso;
22+
} mm_context_t;
23+
24+
#endif /* __ASSEMBLY__ */
25+
26+
#endif /* _ASM_RISCV_MMU_H */

arch/riscv/include/asm/ptrace.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (C) 2012 Regents of the University of California
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, version 2.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*/
13+
14+
#ifndef _ASM_RISCV_PTRACE_H
15+
#define _ASM_RISCV_PTRACE_H
16+
17+
#include <uapi/asm/ptrace.h>
18+
#include <asm/csr.h>
19+
20+
#ifndef __ASSEMBLY__
21+
22+
struct pt_regs {
23+
unsigned long sepc;
24+
unsigned long ra;
25+
unsigned long sp;
26+
unsigned long gp;
27+
unsigned long tp;
28+
unsigned long t0;
29+
unsigned long t1;
30+
unsigned long t2;
31+
unsigned long s0;
32+
unsigned long s1;
33+
unsigned long a0;
34+
unsigned long a1;
35+
unsigned long a2;
36+
unsigned long a3;
37+
unsigned long a4;
38+
unsigned long a5;
39+
unsigned long a6;
40+
unsigned long a7;
41+
unsigned long s2;
42+
unsigned long s3;
43+
unsigned long s4;
44+
unsigned long s5;
45+
unsigned long s6;
46+
unsigned long s7;
47+
unsigned long s8;
48+
unsigned long s9;
49+
unsigned long s10;
50+
unsigned long s11;
51+
unsigned long t3;
52+
unsigned long t4;
53+
unsigned long t5;
54+
unsigned long t6;
55+
/* Supervisor CSRs */
56+
unsigned long sstatus;
57+
unsigned long sbadaddr;
58+
unsigned long scause;
59+
/* a0 value before the syscall */
60+
unsigned long orig_a0;
61+
};
62+
63+
#ifdef CONFIG_64BIT
64+
#define REG_FMT "%016lx"
65+
#else
66+
#define REG_FMT "%08lx"
67+
#endif
68+
69+
#define user_mode(regs) (((regs)->sstatus & SR_PS) == 0)
70+
71+
72+
/* Helpers for working with the instruction pointer */
73+
#define GET_IP(regs) ((regs)->sepc)
74+
#define SET_IP(regs, val) (GET_IP(regs) = (val))
75+
76+
static inline unsigned long instruction_pointer(struct pt_regs *regs)
77+
{
78+
return GET_IP(regs);
79+
}
80+
static inline void instruction_pointer_set(struct pt_regs *regs,
81+
unsigned long val)
82+
{
83+
SET_IP(regs, val);
84+
}
85+
86+
#define profile_pc(regs) instruction_pointer(regs)
87+
88+
/* Helpers for working with the user stack pointer */
89+
#define GET_USP(regs) ((regs)->sp)
90+
#define SET_USP(regs, val) (GET_USP(regs) = (val))
91+
92+
static inline unsigned long user_stack_pointer(struct pt_regs *regs)
93+
{
94+
return GET_USP(regs);
95+
}
96+
static inline void user_stack_pointer_set(struct pt_regs *regs,
97+
unsigned long val)
98+
{
99+
SET_USP(regs, val);
100+
}
101+
102+
/* Helpers for working with the frame pointer */
103+
#define GET_FP(regs) ((regs)->s0)
104+
#define SET_FP(regs, val) (GET_FP(regs) = (val))
105+
106+
static inline unsigned long frame_pointer(struct pt_regs *regs)
107+
{
108+
return GET_FP(regs);
109+
}
110+
static inline void frame_pointer_set(struct pt_regs *regs,
111+
unsigned long val)
112+
{
113+
SET_FP(regs, val);
114+
}
115+
116+
#endif /* __ASSEMBLY__ */
117+
118+
#endif /* _ASM_RISCV_PTRACE_H */

arch/riscv/include/asm/syscall.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
3+
* Copyright 2010 Tilera Corporation. All Rights Reserved.
4+
* Copyright 2015 Regents of the University of California, Berkeley
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License
8+
* as published by the Free Software Foundation, version 2.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* See asm-generic/syscall.h for descriptions of what we must do here.
16+
*/
17+
18+
#ifndef _ASM_RISCV_SYSCALL_H
19+
#define _ASM_RISCV_SYSCALL_H
20+
21+
#include <linux/sched.h>
22+
#include <linux/err.h>
23+
24+
/* The array of function pointers for syscalls. */
25+
extern void *sys_call_table[];
26+
27+
/*
28+
* Only the low 32 bits of orig_r0 are meaningful, so we return int.
29+
* This importantly ignores the high bits on 64-bit, so comparisons
30+
* sign-extend the low 32 bits.
31+
*/
32+
static inline int syscall_get_nr(struct task_struct *task,
33+
struct pt_regs *regs)
34+
{
35+
return regs->a7;
36+
}
37+
38+
static inline void syscall_set_nr(struct task_struct *task,
39+
struct pt_regs *regs,
40+
int sysno)
41+
{
42+
regs->a7 = sysno;
43+
}
44+
45+
static inline void syscall_rollback(struct task_struct *task,
46+
struct pt_regs *regs)
47+
{
48+
regs->a0 = regs->orig_a0;
49+
}
50+
51+
static inline long syscall_get_error(struct task_struct *task,
52+
struct pt_regs *regs)
53+
{
54+
unsigned long error = regs->a0;
55+
56+
return IS_ERR_VALUE(error) ? error : 0;
57+
}
58+
59+
static inline long syscall_get_return_value(struct task_struct *task,
60+
struct pt_regs *regs)
61+
{
62+
return regs->a0;
63+
}
64+
65+
static inline void syscall_set_return_value(struct task_struct *task,
66+
struct pt_regs *regs,
67+
int error, long val)
68+
{
69+
regs->a0 = (long) error ?: val;
70+
}
71+
72+
static inline void syscall_get_arguments(struct task_struct *task,
73+
struct pt_regs *regs,
74+
unsigned int i, unsigned int n,
75+
unsigned long *args)
76+
{
77+
BUG_ON(i + n > 6);
78+
if (i == 0) {
79+
args[0] = regs->orig_a0;
80+
args++;
81+
i++;
82+
n--;
83+
}
84+
memcpy(args, &regs->a1 + i * sizeof(regs->a1), n * sizeof(args[0]));
85+
}
86+
87+
static inline void syscall_set_arguments(struct task_struct *task,
88+
struct pt_regs *regs,
89+
unsigned int i, unsigned int n,
90+
const unsigned long *args)
91+
{
92+
BUG_ON(i + n > 6);
93+
if (i == 0) {
94+
regs->orig_a0 = args[0];
95+
args++;
96+
i++;
97+
n--;
98+
}
99+
memcpy(&regs->a1 + i * sizeof(regs->a1), args, n * sizeof(regs->a0));
100+
}
101+
102+
#endif /* _ASM_RISCV_SYSCALL_H */

arch/riscv/include/asm/unistd.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (C) 2012 Regents of the University of California
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, version 2.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*/
13+
14+
#define __ARCH_HAVE_MMU
15+
#define __ARCH_WANT_SYS_CLONE
16+
#include <uapi/asm/unistd.h>

arch/riscv/include/asm/vdso.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2012 ARM Limited
3+
* Copyright (C) 2014 Regents of the University of California
4+
* Copyright (C) 2017 SiFive
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2 as
8+
* published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef _ASM_RISCV_VDSO_H
20+
#define _ASM_RISCV_VDSO_H
21+
22+
#include <linux/types.h>
23+
24+
struct vdso_data {
25+
};
26+
27+
/*
28+
* The VDSO symbols are mapped into Linux so we can just use regular symbol
29+
* addressing to get their offsets in userspace. The symbols are mapped at an
30+
* offset of 0, but since the linker must support setting weak undefined
31+
* symbols to the absolute address 0 it also happens to support other low
32+
* addresses even when the code model suggests those low addresses would not
33+
* otherwise be availiable.
34+
*/
35+
#define VDSO_SYMBOL(base, name) \
36+
({ \
37+
extern const char __vdso_##name[]; \
38+
(void __user *)((unsigned long)(base) + __vdso_##name); \
39+
})
40+
41+
#endif /* _ASM_RISCV_VDSO_H */

arch/riscv/include/uapi/asm/Kbuild

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# UAPI Header export list
2+
include include/uapi/asm-generic/Kbuild.asm
3+
4+
generic-y += setup.h
5+
generic-y += unistd.h
6+
generic-y += errno.h
7+
generic-y += fcntl.h
8+
generic-y += ioctl.h
9+
generic-y += ioctls.h
10+
generic-y += ipcbuf.h
11+
generic-y += mman.h
12+
generic-y += msgbuf.h
13+
generic-y += param.h
14+
generic-y += poll.h
15+
generic-y += posix_types.h
16+
generic-y += resource.h
17+
generic-y += sembuf.h
18+
generic-y += shmbuf.h
19+
generic-y += signal.h
20+
generic-y += socket.h
21+
generic-y += sockios.h
22+
generic-y += stat.h
23+
generic-y += statfs.h
24+
generic-y += swab.h
25+
generic-y += termbits.h
26+
generic-y += termios.h
27+
generic-y += types.h

arch/riscv/include/uapi/asm/auxvec.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2012 ARM Ltd.
3+
* Copyright (C) 2015 Regents of the University of California
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License version 2 as
7+
* published by the Free Software Foundation.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef _UAPI_ASM_RISCV_AUXVEC_H
19+
#define _UAPI_ASM_RISCV_AUXVEC_H
20+
21+
/* vDSO location */
22+
#define AT_SYSINFO_EHDR 33
23+
24+
#endif /* _UAPI_ASM_RISCV_AUXVEC_H */
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (C) 2012 ARM Ltd.
3+
* Copyright (C) 2015 Regents of the University of California
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License version 2 as
7+
* published by the Free Software Foundation.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef _UAPI_ASM_RISCV_BITSPERLONG_H
19+
#define _UAPI_ASM_RISCV_BITSPERLONG_H
20+
21+
#define __BITS_PER_LONG (__SIZEOF_POINTER__ * 8)
22+
23+
#include <asm-generic/bitsperlong.h>
24+
25+
#endif /* _UAPI_ASM_RISCV_BITSPERLONG_H */

0 commit comments

Comments
 (0)