Skip to content

Commit 5d8544e

Browse files
RISC-V: Generic library routines and assembly
This patch contains code that is more specific to the RISC-V ISA than it is to Linux. It contains string and math operations, C wrappers for various assembly instructions, stack walking code, and uaccess. Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent fab957c commit 5d8544e

File tree

11 files changed

+1389
-0
lines changed

11 files changed

+1389
-0
lines changed

arch/riscv/include/asm/asm.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (C) 2015 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_ASM_H
15+
#define _ASM_RISCV_ASM_H
16+
17+
#ifdef __ASSEMBLY__
18+
#define __ASM_STR(x) x
19+
#else
20+
#define __ASM_STR(x) #x
21+
#endif
22+
23+
#if __riscv_xlen == 64
24+
#define __REG_SEL(a, b) __ASM_STR(a)
25+
#elif __riscv_xlen == 32
26+
#define __REG_SEL(a, b) __ASM_STR(b)
27+
#else
28+
#error "Unexpected __riscv_xlen"
29+
#endif
30+
31+
#define REG_L __REG_SEL(ld, lw)
32+
#define REG_S __REG_SEL(sd, sw)
33+
#define SZREG __REG_SEL(8, 4)
34+
#define LGREG __REG_SEL(3, 2)
35+
36+
#if __SIZEOF_POINTER__ == 8
37+
#ifdef __ASSEMBLY__
38+
#define RISCV_PTR .dword
39+
#define RISCV_SZPTR 8
40+
#define RISCV_LGPTR 3
41+
#else
42+
#define RISCV_PTR ".dword"
43+
#define RISCV_SZPTR "8"
44+
#define RISCV_LGPTR "3"
45+
#endif
46+
#elif __SIZEOF_POINTER__ == 4
47+
#ifdef __ASSEMBLY__
48+
#define RISCV_PTR .word
49+
#define RISCV_SZPTR 4
50+
#define RISCV_LGPTR 2
51+
#else
52+
#define RISCV_PTR ".word"
53+
#define RISCV_SZPTR "4"
54+
#define RISCV_LGPTR "2"
55+
#endif
56+
#else
57+
#error "Unexpected __SIZEOF_POINTER__"
58+
#endif
59+
60+
#if (__SIZEOF_INT__ == 4)
61+
#define INT __ASM_STR(.word)
62+
#define SZINT __ASM_STR(4)
63+
#define LGINT __ASM_STR(2)
64+
#else
65+
#error "Unexpected __SIZEOF_INT__"
66+
#endif
67+
68+
#if (__SIZEOF_SHORT__ == 2)
69+
#define SHORT __ASM_STR(.half)
70+
#define SZSHORT __ASM_STR(2)
71+
#define LGSHORT __ASM_STR(1)
72+
#else
73+
#error "Unexpected __SIZEOF_SHORT__"
74+
#endif
75+
76+
#endif /* _ASM_RISCV_ASM_H */

arch/riscv/include/asm/csr.h

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (C) 2015 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_CSR_H
15+
#define _ASM_RISCV_CSR_H
16+
17+
#include <linux/const.h>
18+
19+
/* Status register flags */
20+
#define SR_IE _AC(0x00000002, UL) /* Interrupt Enable */
21+
#define SR_PIE _AC(0x00000020, UL) /* Previous IE */
22+
#define SR_PS _AC(0x00000100, UL) /* Previously Supervisor */
23+
#define SR_SUM _AC(0x00040000, UL) /* Supervisor may access User Memory */
24+
25+
#define SR_FS _AC(0x00006000, UL) /* Floating-point Status */
26+
#define SR_FS_OFF _AC(0x00000000, UL)
27+
#define SR_FS_INITIAL _AC(0x00002000, UL)
28+
#define SR_FS_CLEAN _AC(0x00004000, UL)
29+
#define SR_FS_DIRTY _AC(0x00006000, UL)
30+
31+
#define SR_XS _AC(0x00018000, UL) /* Extension Status */
32+
#define SR_XS_OFF _AC(0x00000000, UL)
33+
#define SR_XS_INITIAL _AC(0x00008000, UL)
34+
#define SR_XS_CLEAN _AC(0x00010000, UL)
35+
#define SR_XS_DIRTY _AC(0x00018000, UL)
36+
37+
#ifndef CONFIG_64BIT
38+
#define SR_SD _AC(0x80000000, UL) /* FS/XS dirty */
39+
#else
40+
#define SR_SD _AC(0x8000000000000000, UL) /* FS/XS dirty */
41+
#endif
42+
43+
/* SPTBR flags */
44+
#if __riscv_xlen == 32
45+
#define SPTBR_PPN _AC(0x003FFFFF, UL)
46+
#define SPTBR_MODE_32 _AC(0x80000000, UL)
47+
#define SPTBR_MODE SPTBR_MODE_32
48+
#else
49+
#define SPTBR_PPN _AC(0x00000FFFFFFFFFFF, UL)
50+
#define SPTBR_MODE_39 _AC(0x8000000000000000, UL)
51+
#define SPTBR_MODE SPTBR_MODE_39
52+
#endif
53+
54+
/* Interrupt Enable and Interrupt Pending flags */
55+
#define SIE_SSIE _AC(0x00000002, UL) /* Software Interrupt Enable */
56+
#define SIE_STIE _AC(0x00000020, UL) /* Timer Interrupt Enable */
57+
58+
#define EXC_INST_MISALIGNED 0
59+
#define EXC_INST_ACCESS 1
60+
#define EXC_BREAKPOINT 3
61+
#define EXC_LOAD_ACCESS 5
62+
#define EXC_STORE_ACCESS 7
63+
#define EXC_SYSCALL 8
64+
#define EXC_INST_PAGE_FAULT 12
65+
#define EXC_LOAD_PAGE_FAULT 13
66+
#define EXC_STORE_PAGE_FAULT 15
67+
68+
#ifndef __ASSEMBLY__
69+
70+
#define csr_swap(csr, val) \
71+
({ \
72+
unsigned long __v = (unsigned long)(val); \
73+
__asm__ __volatile__ ("csrrw %0, " #csr ", %1" \
74+
: "=r" (__v) : "rK" (__v) \
75+
: "memory"); \
76+
__v; \
77+
})
78+
79+
#define csr_read(csr) \
80+
({ \
81+
register unsigned long __v; \
82+
__asm__ __volatile__ ("csrr %0, " #csr \
83+
: "=r" (__v) : \
84+
: "memory"); \
85+
__v; \
86+
})
87+
88+
#define csr_write(csr, val) \
89+
({ \
90+
unsigned long __v = (unsigned long)(val); \
91+
__asm__ __volatile__ ("csrw " #csr ", %0" \
92+
: : "rK" (__v) \
93+
: "memory"); \
94+
})
95+
96+
#define csr_read_set(csr, val) \
97+
({ \
98+
unsigned long __v = (unsigned long)(val); \
99+
__asm__ __volatile__ ("csrrs %0, " #csr ", %1" \
100+
: "=r" (__v) : "rK" (__v) \
101+
: "memory"); \
102+
__v; \
103+
})
104+
105+
#define csr_set(csr, val) \
106+
({ \
107+
unsigned long __v = (unsigned long)(val); \
108+
__asm__ __volatile__ ("csrs " #csr ", %0" \
109+
: : "rK" (__v) \
110+
: "memory"); \
111+
})
112+
113+
#define csr_read_clear(csr, val) \
114+
({ \
115+
unsigned long __v = (unsigned long)(val); \
116+
__asm__ __volatile__ ("csrrc %0, " #csr ", %1" \
117+
: "=r" (__v) : "rK" (__v) \
118+
: "memory"); \
119+
__v; \
120+
})
121+
122+
#define csr_clear(csr, val) \
123+
({ \
124+
unsigned long __v = (unsigned long)(val); \
125+
__asm__ __volatile__ ("csrc " #csr ", %0" \
126+
: : "rK" (__v) \
127+
: "memory"); \
128+
})
129+
130+
#endif /* __ASSEMBLY__ */
131+
132+
#endif /* _ASM_RISCV_CSR_H */

arch/riscv/include/asm/linkage.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) 2015 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_LINKAGE_H
15+
#define _ASM_RISCV_LINKAGE_H
16+
17+
#define __ALIGN .balign 4
18+
#define __ALIGN_STR ".balign 4"
19+
20+
#endif /* _ASM_RISCV_LINKAGE_H */

arch/riscv/include/asm/string.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2013 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_STRING_H
15+
#define _ASM_RISCV_STRING_H
16+
17+
#include <linux/types.h>
18+
#include <linux/linkage.h>
19+
20+
#define __HAVE_ARCH_MEMSET
21+
extern asmlinkage void *memset(void *, int, size_t);
22+
23+
#define __HAVE_ARCH_MEMCPY
24+
extern asmlinkage void *memcpy(void *, const void *, size_t);
25+
26+
#endif /* _ASM_RISCV_STRING_H */

0 commit comments

Comments
 (0)