Skip to contentSkip to conversation

RISC-V Linux Port v4

PATCH In review Palmer Dabbelt 10 participants 43 messages Jul 4, 2017 – Jul 11, 2017 2 messages not in the archive

Subsystems (9)
lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH 1/9 RISC-V: Init and Halt Code

In reply to Palmer Dabbelt

This contains the various __init C functions, the initial assembly kernel entry point, and the code to reset the system. When a file was init-related, it contains

---
 arch/riscv/include/asm/bug.h   |  88 +++++++++++++++
 arch/riscv/include/asm/cache.h |  22 ++++
 arch/riscv/include/asm/smp.h   |  41 +++++++
 arch/riscv/kernel/cacheinfo.c  | 105 ++++++++++++++++++
 arch/riscv/kernel/cpu.c        |  89 +++++++++++++++
 arch/riscv/kernel/head.S       | 152 +++++++++++++++++++++++++
 arch/riscv/kernel/irq.c        |  20 ++++
 arch/riscv/kernel/reset.c      |  36 ++++++
 arch/riscv/kernel/setup.c      | 246 +++++++++++++++++++++++++++++++++++++++++
 arch/riscv/kernel/smp.c        | 110 ++++++++++++++++++
 arch/riscv/kernel/smpboot.c    | 114 +++++++++++++++++++
 arch/riscv/kernel/time.c       |  61 ++++++++++
 arch/riscv/kernel/traps.c      | 184 ++++++++++++++++++++++++++++++
 arch/riscv/kernel/vdso.c       | 125 +++++++++++++++++++++
 arch/riscv/mm/init.c           |  70 ++++++++++++
 15 files changed, 1463 insertions(+)
 create mode 100644 arch/riscv/include/asm/bug.h
 create mode 100644 arch/riscv/include/asm/cache.h
 create mode 100644 arch/riscv/include/asm/smp.h
 create mode 100644 arch/riscv/kernel/cacheinfo.c
 create mode 100644 arch/riscv/kernel/cpu.c
 create mode 100644 arch/riscv/kernel/head.S
 create mode 100644 arch/riscv/kernel/irq.c
 create mode 100644 arch/riscv/kernel/reset.c
 create mode 100644 arch/riscv/kernel/setup.c
 create mode 100644 arch/riscv/kernel/smp.c
 create mode 100644 arch/riscv/kernel/smpboot.c
 create mode 100644 arch/riscv/kernel/time.c
 create mode 100644 arch/riscv/kernel/traps.c
 create mode 100644 arch/riscv/kernel/vdso.c
 create mode 100644 arch/riscv/mm/init.c
arch/riscv/include/asm/bug.h new file +88 -0
new file mode 100644
index 000000000000..e2f690c20729
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_BUG_H
+#define _ASM_RISCV_BUG_H
+
+#include <linux/compiler.h>
+#include <linux/const.h>
+#include <linux/types.h>
+
+#include <asm/asm.h>
+
+#ifdef CONFIG_GENERIC_BUG
+#define __BUG_INSN	_AC(0x00100073, UL) /* sbreak */
+
+#ifndef __ASSEMBLY__
+typedef u32 bug_insn_t;
+
+#ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
+#define __BUG_ENTRY_ADDR	INT " 1b - 2b"
+#define __BUG_ENTRY_FILE	INT " %0 - 2b"
+#else
+#define __BUG_ENTRY_ADDR	RISCV_PTR " 1b"
+#define __BUG_ENTRY_FILE	RISCV_PTR " %0"
+#endif
+
+#ifdef CONFIG_DEBUG_BUGVERBOSE
+#define __BUG_ENTRY			\
+	__BUG_ENTRY_ADDR "\n\t"		\
+	__BUG_ENTRY_FILE "\n\t"		\
+	SHORT " %1"
+#else
+#define __BUG_ENTRY			\
+	__BUG_ENTRY_ADDR
+#endif
+
+#define BUG()							\
+do {								\
+	__asm__ __volatile__ (					\
+		"1:\n\t"					\
+			"sbreak\n"				\
+			".pushsection __bug_table,\"a\"\n\t"	\
+		"2:\n\t"					\
+			__BUG_ENTRY "\n\t"			\
+			".org 2b + %2\n\t"			\
+			".popsection"				\
+		:						\
+		: "i" (__FILE__), "i" (__LINE__),		\
+		  "i" (sizeof(struct bug_entry)));		\
+	unreachable();						\
+} while (0)
+#endif /* !__ASSEMBLY__ */
+#else /* CONFIG_GENERIC_BUG */
+#ifndef __ASSEMBLY__
+#define BUG()							\
+do {								\
+	__asm__ __volatile__ ("sbreak\n");			\
+	unreachable();						\
+} while (0)
+#endif /* !__ASSEMBLY__ */
+#endif /* CONFIG_GENERIC_BUG */
+
+#define HAVE_ARCH_BUG
+
+#include <asm-generic/bug.h>
+
+#ifndef __ASSEMBLY__
+
+struct pt_regs;
+struct task_struct;
+
+extern void die(struct pt_regs *regs, const char *str);
+extern void do_trap(struct pt_regs *regs, int signo, int code,
+	unsigned long addr, struct task_struct *tsk);
+
+#endif /* !__ASSEMBLY__ */
+
+#endif /* _ASM_RISCV_BUG_H */
arch/riscv/include/asm/cache.h new file +22 -0
new file mode 100644
index 000000000000..e8f0d1110d74
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2017 Chen Liqin <[email protected]>
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_CACHE_H
+#define _ASM_RISCV_CACHE_H
+
+#define L1_CACHE_SHIFT		6
+
+#define L1_CACHE_BYTES		(1 << L1_CACHE_SHIFT)
+
+#endif /* _ASM_RISCV_CACHE_H */
arch/riscv/include/asm/smp.h new file +41 -0

41 lines not on this page · raw message

arch/riscv/kernel/cacheinfo.c new file +105 -0

105 lines not on this page · raw message

arch/riscv/kernel/cpu.c new file +89 -0

89 lines not on this page · raw message

arch/riscv/kernel/head.S new file +152 -0

152 lines not on this page · raw message

arch/riscv/kernel/irq.c new file +20 -0
new file mode 100644
index 000000000000..737d7cce2c6d
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2017 SiFive
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/irqchip.h>
+
+void __init init_IRQ(void)
+{
+	irqchip_init();
+}
arch/riscv/kernel/reset.c new file +36 -0
new file mode 100644
index 000000000000..2a53d26ffdd6
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/reboot.h>
+#include <linux/export.h>
+#include <asm/sbi.h>
+
+void (*pm_power_off)(void) = machine_power_off;
+EXPORT_SYMBOL(pm_power_off);
+
+void machine_restart(char *cmd)
+{
+	do_kernel_restart(cmd);
+	while (1);
+}
+
+void machine_halt(void)
+{
+	machine_power_off();
+}
+
+void machine_power_off(void)
+{
+	sbi_shutdown();
+	while (1);
+}
arch/riscv/kernel/setup.c new file +246 -0

246 lines not on this page · raw message

arch/riscv/kernel/smp.c new file +110 -0

110 lines not on this page · raw message

arch/riscv/kernel/smpboot.c new file +114 -0

114 lines not on this page · raw message

arch/riscv/kernel/time.c new file +61 -0

61 lines not on this page · raw message

arch/riscv/kernel/traps.c new file +184 -0

184 lines not on this page · raw message

arch/riscv/kernel/vdso.c new file +125 -0

125 lines not on this page · raw message

arch/riscv/mm/init.c new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#10 lore raw

PATCH 2/9 RISC-V: Atomic and Locking Code

In reply to Palmer Dabbelt

This contains all the code that directly interfaces with the RISC-V memory model. While this code corforms to the current RISC-V ISA specifications (user 2.2 and priv 1.10), the memory model is somewhat underspecified in those documents. There is a working group that hopes to produce a formal memory model by the end of the year, but my understanding is that the basic definitions we're relying on here won't change significantly.

---
 arch/riscv/include/asm/atomic.h         | 337 ++++++++++++++++++++++++++++++++
 arch/riscv/include/asm/barrier.h        | 143 ++++++++++++++
 arch/riscv/include/asm/bitops.h         | 228 +++++++++++++++++++++
 arch/riscv/include/asm/cacheflush.h     |  39 ++++
 arch/riscv/include/asm/cmpxchg.h        | 138 +++++++++++++
 arch/riscv/include/asm/io.h             | 180 +++++++++++++++++
 arch/riscv/include/asm/spinlock.h       | 167 ++++++++++++++++
 arch/riscv/include/asm/spinlock_types.h |  33 ++++
 arch/riscv/include/asm/tlb.h            |  24 +++
 arch/riscv/include/asm/tlbflush.h       |  64 ++++++
 10 files changed, 1353 insertions(+)
 create mode 100644 arch/riscv/include/asm/atomic.h
 create mode 100644 arch/riscv/include/asm/barrier.h
 create mode 100644 arch/riscv/include/asm/bitops.h
 create mode 100644 arch/riscv/include/asm/cacheflush.h
 create mode 100644 arch/riscv/include/asm/cmpxchg.h
 create mode 100644 arch/riscv/include/asm/io.h
 create mode 100644 arch/riscv/include/asm/spinlock.h
 create mode 100644 arch/riscv/include/asm/spinlock_types.h
 create mode 100644 arch/riscv/include/asm/tlb.h
 create mode 100644 arch/riscv/include/asm/tlbflush.h
arch/riscv/include/asm/atomic.h new file +337 -0
new file mode 100644
index 000000000000..e5a12cfa405a
@@ -0,0 +1,337 @@
+/*
+ * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2017 SiFive
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public Licence
+ * as published by the Free Software Foundation; either version
+ * 2 of the Licence, or (at your option) any later version.
+ */
+
+#ifndef _ASM_RISCV_ATOMIC_H
+#define _ASM_RISCV_ATOMIC_H
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+# include <asm-generic/atomic64.h>
+#else
+# if (__riscv_xlen < 64)
+#  error "64-bit atomics require XLEN to be at least 64"
+# endif
+#endif
+
+#ifdef CONFIG_ISA_A
+
+#include <asm/cmpxchg.h>
+#include <asm/barrier.h>
+
+#define ATOMIC_INIT(i)	{ (i) }
+static __always_inline int atomic_read(const atomic_t *v)
+{
+	return READ_ONCE(v->counter);
+}
+static __always_inline void atomic_set(atomic_t *v, int i)
+{
+	WRITE_ONCE(v->counter, i);
+}
+
+#ifndef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC64_INIT(i) { (i) }
+static __always_inline int atomic64_read(const atomic64_t *v)
+{
+	return READ_ONCE(v->counter);
+}
+static __always_inline void atomic64_set(atomic64_t *v, int i)
+{
+	WRITE_ONCE(v->counter, i);
+}
+#endif
+
+/*
+ * First, the atomic ops that have no ordering constraints and therefor don't
+ * have the AQ or RL bits set.  These don't return anything, so there's only
+ * one version to worry about.
+ */
+#define ATOMIC_OP(op, asm_op, c_op, I, asm_type, c_type, prefix)				\
+static __always_inline void atomic##prefix##_##op(c_type i, atomic##prefix##_t *v)		\
+{												\
+	__asm__ __volatile__ (									\
+		"amo" #asm_op "." #asm_type " zero, %1, %0"					\
+		: "+A" (v->counter)								\
+		: "r" (I));									\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, asm_op, c_op, I)			\
+        ATOMIC_OP (op, asm_op, c_op, I, w,  int,   )
+#else
+#define ATOMIC_OPS(op, asm_op, c_op, I)			\
+        ATOMIC_OP (op, asm_op, c_op, I, w,  int,   )	\
+        ATOMIC_OP (op, asm_op, c_op, I, d, long, 64)
+#endif
+
+ATOMIC_OPS(add, add, +,  i)
+ATOMIC_OPS(sub, add, +, -i)
+/*
+ * FIXME: I could only find documentation that atomic_{add,sub,inc,dec} are
+ * barrier-free.  I'm assuming that and/or/xor have the same constraints as the
+ * others.
+ */
+ATOMIC_OPS(and, and, &,  i)
+ATOMIC_OPS( or,  or, |,  i)
+ATOMIC_OPS(xor, xor, ^,  i)
+
+#undef ATOMIC_OP
+#undef ATOMIC_OPS
+
+/*
+ * Atomic ops that have ordered, relaxed, acquire, and relese variants.
+ * There's two flavors of these: the arithmatic ops have both fetch and return
+ * versions, while the logical ops only have fetch versions.
+ */
+#define ATOMIC_FETCH_OP(op, asm_op, c_op, I, asm_or, c_or, asm_type, c_type, prefix)			\
+static __always_inline c_type atomic##prefix##_fetch_##op##c_or(c_type i, atomic##prefix##_t *v)	\
+{													\
+	register c_type ret;										\
+	__asm__ __volatile__ (										\
+		"amo" #asm_op "." #asm_type #asm_or " %2, %1, %0"					\
+		: "+A" (v->counter), "=r" (ret)								\
+		: "r" (I));										\
+	return ret;											\
+}
+
+#define ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_or, c_or, asm_type, c_type, prefix)			\
+static __always_inline c_type atomic##prefix##_##op##_return##c_or(int i, atomic##prefix##_t *v)	\
+{													\
+        return atomic##prefix##_fetch_##op##c_or(i, v) c_op I;						\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, asm_op, c_op, I, asm_or, c_or)				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I, asm_or, c_or, w,  int,   )	\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_or, c_or, w,  int,   )
+#else
+#define ATOMIC_OPS(op, asm_op, c_op, I, asm_or, c_or)				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I, asm_or, c_or, w,  int,   )	\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_or, c_or, w,  int,   )	\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I, asm_or, c_or, d, long, 64)	\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_or, c_or, d, long, 64)
+#endif
+
+ATOMIC_OPS(add, add, +,  i,      , _relaxed)
+ATOMIC_OPS(add, add, +,  i, .aq  , _acquire)
+ATOMIC_OPS(add, add, +,  i, .rl  , _release)
+ATOMIC_OPS(add, add, +,  i, .aqrl,         )
+
+ATOMIC_OPS(sub, add, +, -i,      , _relaxed)
+ATOMIC_OPS(sub, add, +, -i, .aq  , _acquire)
+ATOMIC_OPS(sub, add, +, -i, .rl  , _release)
+ATOMIC_OPS(sub, add, +, -i, .aqrl,         )
+
+#undef ATOMIC_OPS
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, asm_op, c_op, I, asm_or, c_or)				\
+        ATOMIC_FETCH_OP(op, asm_op, c_op, I, asm_or, c_or, w,  int,   )
+#else
+#define ATOMIC_OPS(op, asm_op, c_op, I, asm_or, c_or)				\
+        ATOMIC_FETCH_OP(op, asm_op, c_op, I, asm_or, c_or, w,  int,   )		\
+        ATOMIC_FETCH_OP(op, asm_op, c_op, I, asm_or, c_or, d, long, 64)
+#endif
+
+ATOMIC_OPS(and, and, &,  i,      , _relaxed)
+ATOMIC_OPS(and, and, &,  i, .aq  , _acquire)
+ATOMIC_OPS(and, and, &,  i, .rl  , _release)
+ATOMIC_OPS(and, and, &,  i, .aqrl,         )
+
+ATOMIC_OPS( or,  or, |,  i,      , _relaxed)
+ATOMIC_OPS( or,  or, |,  i, .aq  , _acquire)
+ATOMIC_OPS( or,  or, |,  i, .rl  , _release)
+ATOMIC_OPS( or,  or, |,  i, .aqrl,         )
+
+ATOMIC_OPS(xor, xor, ^,  i,      , _relaxed)
+ATOMIC_OPS(xor, xor, ^,  i, .aq  , _acquire)
+ATOMIC_OPS(xor, xor, ^,  i, .rl  , _release)
+ATOMIC_OPS(xor, xor, ^,  i, .aqrl,         )
+
+#undef ATOMIC_OPS
+
+#undef ATOMIC_FETCH_OP
+#undef ATOMIC_OP_RETURN
+
+/*
+ * The extra atomic operations that are constructed from one of the core
+ * AMO-based operations above (aside from sub, which is easier to fit above).
+ * These are required to perform a barrier, but they're OK this way because
+ * atomic_*_return is also required to perform a barrier.
+ */
+#define ATOMIC_OP(op, func_op, comp_op, I, c_type, prefix)			\
+static __always_inline bool atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \
+{										\
+	return atomic##prefix##_##func_op##_return(i, v) comp_op I;		\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, func_op, comp_op, I)			\
+        ATOMIC_OP (op, func_op, comp_op, I,  int,   )
+#else
+#define ATOMIC_OPS(op, func_op, comp_op, I)			\
+        ATOMIC_OP (op, func_op, comp_op, I,  int,   )		\
+        ATOMIC_OP (op, func_op, comp_op, I, long, 64)
+#endif
+
+ATOMIC_OPS(add_and_test, add, ==, 0)
+ATOMIC_OPS(sub_and_test, sub, ==, 0)
+ATOMIC_OPS(add_negative, add,  <, 0)
+
+#undef ATOMIC_OP
+#undef ATOMIC_OPS
+
+#define ATOMIC_OP(op, func_op, c_op, I, prefix)					\
+static __always_inline void atomic##prefix##_##op(atomic##prefix##_t *v)	\
+{										\
+	atomic##prefix##_##func_op(I, v);					\
+}
+
+#define ATOMIC_FETCH_OP(op, func_op, c_op, I, prefix)				\
+static __always_inline int atomic##prefix##_fetch_##op(atomic##prefix##_t *v)	\
+{										\
+	return atomic##prefix##_fetch_##func_op(I, v);				\
+}
+
+#define ATOMIC_OP_RETURN(op, asm_op, c_op, I, prefix)				\
+static __always_inline int atomic##prefix##_##op##_return(atomic##prefix##_t *v) \
+{										\
+        return atomic##prefix##_fetch_##op(v) c_op I;				\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, asm_op, c_op, I)						\
+        ATOMIC_OP       (op, asm_op, c_op, I,   )				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I,   )				\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I,   )
+#else
+#define ATOMIC_OPS(op, asm_op, c_op, I)						\
+        ATOMIC_OP       (op, asm_op, c_op, I,   )				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I,   )				\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I,   )				\
+        ATOMIC_OP       (op, asm_op, c_op, I, 64)				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I, 64)				\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I, 64)
+#endif
+
+ATOMIC_OPS(inc, add, +,  1)
+ATOMIC_OPS(dec, add, +, -1)
+
+#undef ATOMIC_OPS
+#undef ATOMIC_OP
+#undef ATOMIC_FETCH_OP
+#undef ATOMIC_OP_RETURN
+
+#define ATOMIC_OP(op, func_op, comp_op, I, prefix)				\
+static __always_inline bool atomic##prefix##_##op(atomic##prefix##_t *v)	\
+{										\
+	return atomic##prefix##_##func_op##_return(v) comp_op I;		\
+}
+
+ATOMIC_OP(inc_and_test, inc, ==, 0,   )
+ATOMIC_OP(dec_and_test, dec, ==, 0,   )
+#ifndef CONFIG_GENERIC_ATOMIC64
+ATOMIC_OP(inc_and_test, inc, ==, 0, 64)
+ATOMIC_OP(dec_and_test, dec, ==, 0, 64)
+#endif
+
+#undef ATOMIC_OP
+
+/* This is required to provide a barrier on success. */
+static __always_inline int __atomic_add_unless(atomic_t *v, int a, int u)
+{
+       register int prev, rc;
+
+	__asm__ __volatile__ (
+		"0:\n\t"
+		"lr.w.aqrl %0, %2\n\t"
+		"beq       %0, %4, 1f\n\t"
+		"add       %1, %0, %3\n\t"
+		"sc.w.aqrl %1, %1, %2\n\t"
+		"bnez      %1, 0b\n\t"
+		"1:"
+		: "=&r" (prev), "=&r" (rc), "+A" (v->counter)
+		: "r" (a), "r" (u));
+	return prev;
+}
+
+#ifndef CONFIG_GENERIC_ATOMIC64
+static __always_inline long atomic64_add_unless(atomic64_t *v, long a, long u)
+{
+       register long prev, rc;
+
+	__asm__ __volatile__ (
+		"0:\n\t"
+		"lr.d.aqrl %0, %2\n\t"
+		"beq       %0, %4, 1f\n\t"
+		"add       %1, %0, %3\n\t"
+		"sc.d.aqrl %1, %1, %2\n\t"
+		"bnez      %1, 0b\n\t"
+		"1:"
+		: "=&r" (prev), "=&r" (rc), "+A" (v->counter)
+		: "r" (a), "r" (u));
+	return prev;
+}
+#endif
+
+/*
+ * The extra atomic operations that are constructed from one of the core
+ * LR/SC-based operations above.
+ */
+static __always_inline int atomic_inc_not_zero(atomic_t *v)
+{
+        return __atomic_add_unless(v, 1, 0);
+}
+
+#ifndef CONFIG_GENERIC_ATOMIC64
+static __always_inline int atomic64_inc_not_zero(atomic64_t *v)
+{
+        return atomic64_add_unless(v, 1, 0);
+}
+#endif
+
+/*
+ * atomic_{cmp,}xchg is required to have exactly the same ordering semantics as
+ * {cmp,}xchg and the operations that return, so they need a barrier.  We just
+ * use the other implementations directly.
+ */
+#define ATOMIC_OP(c_t, prefix, c_or, size, asm_or)						\
+static __always_inline c_t atomic##prefix##_cmpxchg##c_or(atomic##prefix##_t *v, c_t o, c_t n) 	\
+{												\
+	return __cmpxchg(&(v->counter), o, n, size, asm_or, asm_or);				\
+}												\
+static __always_inline c_t atomic##prefix##_xchg##c_or(atomic##prefix##_t *v, c_t n) 		\
+{												\
+	return __xchg(n, &(v->counter), size, asm_or);						\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(c_or, asm_or)			\
+	ATOMIC_OP( int,   , c_or, 4, asm_or)
+#else
+#define ATOMIC_OPS(c_or, asm_or)			\
+	ATOMIC_OP( int,   , c_or, 4, asm_or)		\
+	ATOMIC_OP(long, 64, c_or, 8, asm_or)
+#endif
+
+ATOMIC_OPS(        , .aqrl)
+ATOMIC_OPS(_acquire,   .aq)
+ATOMIC_OPS(_release,   .rl)
+ATOMIC_OPS(_relaxed,      )
+
+#undef ATOMIC_OPS
+#undef ATOMIC_OP
+
+#else /* !CONFIG_ISA_A */
+
+#include <asm-generic/atomic.h>
+
+#endif /* CONFIG_ISA_A */
+
+#endif /* _ASM_RISCV_ATOMIC_H */
arch/riscv/include/asm/barrier.h new file +143 -0

143 lines not on this page · raw message

arch/riscv/include/asm/bitops.h new file +228 -0

228 lines not on this page · raw message

arch/riscv/include/asm/cacheflush.h new file +39 -0
new file mode 100644
index 000000000000..0595585013b0
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_CACHEFLUSH_H
+#define _ASM_RISCV_CACHEFLUSH_H
+
+#include <asm-generic/cacheflush.h>
+
+#undef flush_icache_range
+#undef flush_icache_user_range
+
+static inline void local_flush_icache_all(void)
+{
+	asm volatile ("fence.i" ::: "memory");
+}
+
+#ifndef CONFIG_SMP
+
+#define flush_icache_range(start, end) local_flush_icache_all()
+#define flush_icache_user_range(vma, pg, addr, len) local_flush_icache_all()
+
+#else /* CONFIG_SMP */
+
+#define flush_icache_range(start, end) sbi_remote_fence_i(0)
+#define flush_icache_user_range(vma, pg, addr, len) sbi_remote_fence_i(0)
+
+#endif /* CONFIG_SMP */
+
+#endif /* _ASM_RISCV_CACHEFLUSH_H */
arch/riscv/include/asm/cmpxchg.h new file +138 -0

138 lines not on this page · raw message

arch/riscv/include/asm/io.h new file +180 -0

180 lines not on this page · raw message

arch/riscv/include/asm/spinlock.h new file +167 -0

167 lines not on this page · raw message

arch/riscv/include/asm/spinlock_types.h new file +33 -0
new file mode 100644
index 000000000000..83ac4ac9e2ac
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_SPINLOCK_TYPES_H
+#define _ASM_RISCV_SPINLOCK_TYPES_H
+
+#ifndef __LINUX_SPINLOCK_TYPES_H
+# error "please don't include this file directly"
+#endif
+
+typedef struct {
+	volatile unsigned int lock;
+} arch_spinlock_t;
+
+#define __ARCH_SPIN_LOCK_UNLOCKED	{ 0 }
+
+typedef struct {
+	volatile unsigned int lock;
+} arch_rwlock_t;
+
+#define __ARCH_RW_LOCK_UNLOCKED		{ 0 }
+
+#endif
arch/riscv/include/asm/tlb.h new file +24 -0
new file mode 100644
index 000000000000..c229509288ea
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_TLB_H
+#define _ASM_RISCV_TLB_H
+
+#include <asm-generic/tlb.h>
+
+static inline void tlb_flush(struct mmu_gather *tlb)
+{
+	flush_tlb_mm(tlb->mm);
+}
+
+#endif /* _ASM_RISCV_TLB_H */
arch/riscv/include/asm/tlbflush.h new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#24 lore raw

PATCH 4/9 RISC-V: ELF and module implementation

In reply to Palmer Dabbelt

This patch contains the code that interfaces with ELF objects on RISC-V systems, the vast majority of which is present to load kernel modules.

---
 arch/riscv/include/asm/compat.h | 29 ++++++++++++++
 arch/riscv/include/asm/elf.h    | 83 +++++++++++++++++++++++++++++++++++++++++
 arch/riscv/include/asm/hwcap.h  | 37 ++++++++++++++++++
 arch/riscv/mm/extable.c         | 37 ++++++++++++++++++
 4 files changed, 186 insertions(+)
 create mode 100644 arch/riscv/include/asm/compat.h
 create mode 100644 arch/riscv/include/asm/elf.h
 create mode 100644 arch/riscv/include/asm/hwcap.h
 create mode 100644 arch/riscv/mm/extable.c
arch/riscv/include/asm/compat.h new file +29 -0
new file mode 100644
index 000000000000..044aecff8854
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __ASM_COMPAT_H
+#define __ASM_COMPAT_H
+#ifdef CONFIG_COMPAT
+
+#if defined(CONFIG_64BIT)
+#define COMPAT_UTS_MACHINE "riscv64\0\0"
+#elif defined(CONFIG_32BIT)
+#define COMPAT_UTS_MACHINE "riscv32\0\0"
+#else
+#error "Unknown RISC-V base ISA"
+#endif
+
+#endif /*CONFIG_COMPAT*/
+#endif /*__ASM_COMPAT_H*/
arch/riscv/include/asm/elf.h new file +83 -0

83 lines not on this page · raw message

arch/riscv/include/asm/hwcap.h new file +37 -0
new file mode 100644
index 000000000000..8a4ed7bbcbea
@@ -0,0 +1,37 @@
+/*
+ * Copied from arch/arm64/include/asm/hwcap.h
+ *
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2017 SiFive
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __ASM_HWCAP_H
+#define __ASM_HWCAP_H
+
+#include <uapi/asm/hwcap.h>
+
+#ifndef __ASSEMBLY__
+/*
+ * This yields a mask that user programs can use to figure out what
+ * instruction set this cpu supports.
+ */
+#define ELF_HWCAP		(elf_hwcap)
+
+enum {
+	CAP_HWCAP = 1,
+};
+
+extern unsigned long elf_hwcap;
+#endif
+#endif
arch/riscv/mm/extable.c new file +37 -0
new file mode 100644
index 000000000000..11bb9417123b
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
+ *  Lennox Wu <[email protected]>
+ *  Chen Liqin <[email protected]>
+ * Copyright (C) 2013 Regents of the University of California
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see the file COPYING, or write
+ * to the Free Software Foundation, Inc.,
+ */
+
+
+#include <linux/extable.h>
+#include <linux/module.h>
+#include <linux/uaccess.h>
+
+int fixup_exception(struct pt_regs *regs)
+{
+	const struct exception_table_entry *fixup;
+
+	fixup = search_exception_tables(regs->sepc);
+	if (fixup) {
+		regs->sepc = fixup->fixup;
+		return 1;
+	}
+	return 0;
+}
Signature · 1 line
2.13.0
author maintainer
#25 lore raw

PATCH 5/9 RISC-V: Task implementation

In reply to Palmer Dabbelt

This patch contains the implementation of tasks on RISC-V, most of which is involved in task switching.

---
 arch/riscv/include/asm/asm-offsets.h |   1 +
 arch/riscv/include/asm/current.h     |  44 ++++
 arch/riscv/include/asm/kprobes.h     |  22 ++
 arch/riscv/include/asm/processor.h   |  97 ++++++++
 arch/riscv/include/asm/switch_to.h   |  69 ++++++
 arch/riscv/include/asm/thread_info.h |  93 ++++++++
 arch/riscv/kernel/asm-offsets.c      | 318 +++++++++++++++++++++++++
 arch/riscv/kernel/entry.S            | 450 +++++++++++++++++++++++++++++++++++
 arch/riscv/kernel/process.c          | 132 ++++++++++
 9 files changed, 1226 insertions(+)
 create mode 100644 arch/riscv/include/asm/asm-offsets.h
 create mode 100644 arch/riscv/include/asm/current.h
 create mode 100644 arch/riscv/include/asm/kprobes.h
 create mode 100644 arch/riscv/include/asm/processor.h
 create mode 100644 arch/riscv/include/asm/switch_to.h
 create mode 100644 arch/riscv/include/asm/thread_info.h
 create mode 100644 arch/riscv/kernel/asm-offsets.c
 create mode 100644 arch/riscv/kernel/entry.S
 create mode 100644 arch/riscv/kernel/process.c
arch/riscv/include/asm/asm-offsets.h new file +1 -0
new file mode 100644
index 000000000000..d370ee36a182
@@ -0,0 +1 @@
+#include <generated/asm-offsets.h>
arch/riscv/include/asm/current.h new file +44 -0

44 lines not on this page · raw message

arch/riscv/include/asm/kprobes.h new file +22 -0
new file mode 100644
index 000000000000..c7eb010d1528
@@ -0,0 +1,22 @@
+/*
+ * Copied from arch/arm64/include/asm/kprobes.h
+ *
+ * Copyright (C) 2013 Linaro Limited
+ * Copyright (C) 2017 SiFive
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#ifndef _RISCV_KPROBES_H
+#define _RISCV_KPROBES_H
+
+#include <asm-generic/kprobes.h>
+
+#endif /* _RISCV_KPROBES_H */
arch/riscv/include/asm/processor.h new file +97 -0

97 lines not on this page · raw message

arch/riscv/include/asm/switch_to.h new file +69 -0

69 lines not on this page · raw message

arch/riscv/include/asm/thread_info.h new file +93 -0

93 lines not on this page · raw message

arch/riscv/kernel/asm-offsets.c new file +318 -0

318 lines not on this page · raw message

arch/riscv/kernel/entry.S new file +450 -0

450 lines not on this page · raw message

arch/riscv/kernel/process.c new file +132 -0

132 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#28 lore raw

PATCH 7/9 RISC-V: Paging and MMU

In reply to Palmer Dabbelt

This patch contains code to manage the RISC-V MMU, including definitions of the page tables and the page walking code.

---
 arch/riscv/include/asm/mmu_context.h  |  69 ++++++
 arch/riscv/include/asm/page.h         | 134 +++++++++++
 arch/riscv/include/asm/pgalloc.h      | 124 ++++++++++
 arch/riscv/include/asm/pgtable-32.h   |  25 ++
 arch/riscv/include/asm/pgtable-64.h   |  84 +++++++
 arch/riscv/include/asm/pgtable-bits.h |  48 ++++
 arch/riscv/include/asm/pgtable.h      | 430 ++++++++++++++++++++++++++++++++++
 arch/riscv/mm/fault.c                 | 282 ++++++++++++++++++++++
 8 files changed, 1196 insertions(+)
 create mode 100644 arch/riscv/include/asm/mmu_context.h
 create mode 100644 arch/riscv/include/asm/page.h
 create mode 100644 arch/riscv/include/asm/pgalloc.h
 create mode 100644 arch/riscv/include/asm/pgtable-32.h
 create mode 100644 arch/riscv/include/asm/pgtable-64.h
 create mode 100644 arch/riscv/include/asm/pgtable-bits.h
 create mode 100644 arch/riscv/include/asm/pgtable.h
 create mode 100644 arch/riscv/mm/fault.c
arch/riscv/include/asm/mmu_context.h new file +69 -0
new file mode 100644
index 000000000000..de1fc1631fc4
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_MMU_CONTEXT_H
+#define _ASM_RISCV_MMU_CONTEXT_H
+
+#include <asm-generic/mm_hooks.h>
+
+#include <linux/mm.h>
+#include <linux/sched.h>
+#include <asm/tlbflush.h>
+
+static inline void enter_lazy_tlb(struct mm_struct *mm,
+	struct task_struct *task)
+{
+}
+
+/* Initialize context-related info for a new mm_struct */
+static inline int init_new_context(struct task_struct *task,
+	struct mm_struct *mm)
+{
+	return 0;
+}
+
+static inline void destroy_context(struct mm_struct *mm)
+{
+}
+
+static inline pgd_t *current_pgdir(void)
+{
+	return pfn_to_virt(csr_read(sptbr) & SPTBR_PPN);
+}
+
+static inline void set_pgdir(pgd_t *pgd)
+{
+	csr_write(sptbr, virt_to_pfn(pgd) | SPTBR_MODE);
+}
+
+static inline void switch_mm(struct mm_struct *prev,
+	struct mm_struct *next, struct task_struct *task)
+{
+	if (likely(prev != next)) {
+		set_pgdir(next->pgd);
+		local_flush_tlb_all();
+	}
+}
+
+static inline void activate_mm(struct mm_struct *prev,
+			       struct mm_struct *next)
+{
+	switch_mm(prev, next, NULL);
+}
+
+static inline void deactivate_mm(struct task_struct *task,
+	struct mm_struct *mm)
+{
+}
+
+#endif /* _ASM_RISCV_MMU_CONTEXT_H */
arch/riscv/include/asm/page.h new file +134 -0

134 lines not on this page · raw message

arch/riscv/include/asm/pgalloc.h new file +124 -0

124 lines not on this page · raw message

arch/riscv/include/asm/pgtable-32.h new file +25 -0
new file mode 100644
index 000000000000..d61974b74182
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_PGTABLE_32_H
+#define _ASM_RISCV_PGTABLE_32_H
+
+#include <asm-generic/pgtable-nopmd.h>
+#include <linux/const.h>
+
+/* Size of region mapped by a page global directory */
+#define PGDIR_SHIFT     22
+#define PGDIR_SIZE      (_AC(1, UL) << PGDIR_SHIFT)
+#define PGDIR_MASK      (~(PGDIR_SIZE - 1))
+
+#endif /* _ASM_RISCV_PGTABLE_32_H */
arch/riscv/include/asm/pgtable-64.h new file +84 -0

84 lines not on this page · raw message

arch/riscv/include/asm/pgtable-bits.h new file +48 -0

48 lines not on this page · raw message

arch/riscv/include/asm/pgtable.h new file +430 -0

430 lines not on this page · raw message

arch/riscv/mm/fault.c new file +282 -0

282 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#30 lore raw

PATCH 8/9 RISC-V: User-facing API

In reply to Palmer Dabbelt

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.

---
 arch/riscv/include/asm/elf.h              |   3 +-
 arch/riscv/include/asm/mmu.h              |  26 +++
 arch/riscv/include/asm/ptrace.h           | 116 ++++++++++++
 arch/riscv/include/asm/syscall.h          |  90 ++++++++++
 arch/riscv/include/asm/syscalls.h         |  25 +++
 arch/riscv/include/asm/unistd.h           |  16 ++
 arch/riscv/include/asm/vdso.h             |  32 ++++
 arch/riscv/include/uapi/asm/auxvec.h      |  24 +++
 arch/riscv/include/uapi/asm/bitsperlong.h |  25 +++
 arch/riscv/include/uapi/asm/byteorder.h   |  23 +++
 arch/riscv/include/uapi/asm/elf.h         |  83 +++++++++
 arch/riscv/include/uapi/asm/hwcap.h       |  36 ++++
 arch/riscv/include/uapi/asm/ptrace.h      |  90 ++++++++++
 arch/riscv/include/uapi/asm/sigcontext.h  |  30 ++++
 arch/riscv/include/uapi/asm/siginfo.h     |  24 +++
 arch/riscv/include/uapi/asm/ucontext.h    |  35 ++++
 arch/riscv/include/uapi/asm/unistd.h      |  23 +++
 arch/riscv/kernel/cpufeature.c            |  61 +++++++
 arch/riscv/kernel/module.c                | 217 ++++++++++++++++++++++
 arch/riscv/kernel/ptrace.c                | 138 ++++++++++++++
 arch/riscv/kernel/riscv_ksyms.c           |  15 ++
 arch/riscv/kernel/signal.c                | 289 ++++++++++++++++++++++++++++++
 arch/riscv/kernel/sys_riscv.c             |  90 ++++++++++
 arch/riscv/kernel/syscall_table.c         |  25 +++
 arch/riscv/kernel/vdso/.gitignore         |   1 +
 arch/riscv/kernel/vdso/cmpxchg32.S        |  40 +++++
 arch/riscv/kernel/vdso/cmpxchg64.S        |  40 +++++
 arch/riscv/kernel/vdso/sigreturn.S        |  24 +++
 arch/riscv/kernel/vdso/vdso.S             |  27 +++
 29 files changed, 1667 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/include/asm/mmu.h
 create mode 100644 arch/riscv/include/asm/ptrace.h
 create mode 100644 arch/riscv/include/asm/syscall.h
 create mode 100644 arch/riscv/include/asm/syscalls.h
 create mode 100644 arch/riscv/include/asm/unistd.h
 create mode 100644 arch/riscv/include/asm/vdso.h
 create mode 100644 arch/riscv/include/uapi/asm/auxvec.h
 create mode 100644 arch/riscv/include/uapi/asm/bitsperlong.h
 create mode 100644 arch/riscv/include/uapi/asm/byteorder.h
 create mode 100644 arch/riscv/include/uapi/asm/elf.h
 create mode 100644 arch/riscv/include/uapi/asm/hwcap.h
 create mode 100644 arch/riscv/include/uapi/asm/ptrace.h
 create mode 100644 arch/riscv/include/uapi/asm/sigcontext.h
 create mode 100644 arch/riscv/include/uapi/asm/siginfo.h
 create mode 100644 arch/riscv/include/uapi/asm/ucontext.h
 create mode 100644 arch/riscv/include/uapi/asm/unistd.h
 create mode 100644 arch/riscv/kernel/cpufeature.c
 create mode 100644 arch/riscv/kernel/module.c
 create mode 100644 arch/riscv/kernel/ptrace.c
 create mode 100644 arch/riscv/kernel/riscv_ksyms.c
 create mode 100644 arch/riscv/kernel/signal.c
 create mode 100644 arch/riscv/kernel/sys_riscv.c
 create mode 100644 arch/riscv/kernel/syscall_table.c
 create mode 100644 arch/riscv/kernel/vdso/.gitignore
 create mode 100644 arch/riscv/kernel/vdso/cmpxchg32.S
 create mode 100644 arch/riscv/kernel/vdso/cmpxchg64.S
 create mode 100644 arch/riscv/kernel/vdso/sigreturn.S
 create mode 100644 arch/riscv/kernel/vdso/vdso.S
arch/riscv/include/asm/elf.h +2 -1
index 5ded3f6f83ea..a1ef503d616e 100644
@@ -59,7 +59,8 @@
  * instruction set this CPU supports.  This could be done in user space,
  * but it's not easy, and we've already done it here.
  */
-#define ELF_HWCAP	(0)
+#define ELF_HWCAP	(elf_hwcap)
+extern unsigned long elf_hwcap;
 
 /*
  * This yields a string that ld.so will use to load implementation
arch/riscv/include/asm/mmu.h new file +26 -0
new file mode 100644
index 000000000000..66805cba9a27
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+
+#ifndef _ASM_RISCV_MMU_H
+#define _ASM_RISCV_MMU_H
+
+#ifndef __ASSEMBLY__
+
+typedef struct {
+	void *vdso;
+} mm_context_t;
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* _ASM_RISCV_MMU_H */
arch/riscv/include/asm/ptrace.h new file +116 -0

116 lines not on this page · raw message

arch/riscv/include/asm/syscall.h new file +90 -0

90 lines not on this page · raw message

arch/riscv/include/asm/syscalls.h new file +25 -0
new file mode 100644
index 000000000000..6490274fbb76
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2014 Darius Rad <[email protected]>
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_SYSCALLS_H
+#define _ASM_RISCV_SYSCALLS_H
+
+#include <linux/linkage.h>
+
+#include <asm-generic/syscalls.h>
+
+/* kernel/sys_riscv.c */
+asmlinkage long sys_sysriscv_cmpxchg32(u32 __user * ptr, u32 new, u32 old);
+asmlinkage long sys_sysriscv_cmpxchg64(u64 __user * ptr, u64 new, u64 old);
+
+#endif /* _ASM_RISCV_SYSCALLS_H */
arch/riscv/include/asm/unistd.h new file +16 -0
new file mode 100644
index 000000000000..9f250ed007cd
@@ -0,0 +1,16 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#define __ARCH_HAVE_MMU
+#define __ARCH_WANT_SYS_CLONE
+#include <uapi/asm/unistd.h>
arch/riscv/include/asm/vdso.h new file +32 -0
new file mode 100644
index 000000000000..95768a3810a7
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2012 ARM Limited
+ * Copyright (C) 2014 Regents of the University of California
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _ASM_RISCV_VDSO_H
+#define _ASM_RISCV_VDSO_H
+
+#include <linux/types.h>
+
+struct vdso_data {
+};
+
+#define VDSO_SYMBOL(base, name)					\
+({								\
+	extern const char __vdso_##name[];			\
+	(void __user *)((unsigned long)(base) + __vdso_##name);	\
+})
+
+#endif /* _ASM_RISCV_VDSO_H */
arch/riscv/include/uapi/asm/auxvec.h new file +24 -0
new file mode 100644
index 000000000000..1376515547cd
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _UAPI_ASM_RISCV_AUXVEC_H
+#define _UAPI_ASM_RISCV_AUXVEC_H
+
+/* vDSO location */
+#define AT_SYSINFO_EHDR 33
+
+#endif /* _UAPI_ASM_RISCV_AUXVEC_H */
arch/riscv/include/uapi/asm/bitsperlong.h new file +25 -0
new file mode 100644
index 000000000000..0b3cb52fd29d
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _UAPI_ASM_RISCV_BITSPERLONG_H
+#define _UAPI_ASM_RISCV_BITSPERLONG_H
+
+#define __BITS_PER_LONG (__SIZEOF_POINTER__ * 8)
+
+#include <asm-generic/bitsperlong.h>
+
+#endif /* _UAPI_ASM_RISCV_BITSPERLONG_H */
arch/riscv/include/uapi/asm/byteorder.h new file +23 -0
new file mode 100644
index 000000000000..4ca38af2cd32
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _UAPI_ASM_RISCV_BYTEORDER_H
+#define _UAPI_ASM_RISCV_BYTEORDER_H
+
+#include <linux/byteorder/little_endian.h>
+
+#endif /* _UAPI_ASM_RISCV_BYTEORDER_H */
arch/riscv/include/uapi/asm/elf.h new file +83 -0

83 lines not on this page · raw message

arch/riscv/include/uapi/asm/hwcap.h new file +36 -0
new file mode 100644
index 000000000000..f333221c9ab2
@@ -0,0 +1,36 @@
+/*
+ * Copied from arch/arm64/include/asm/hwcap.h
+ *
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2017 SiFive
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __UAPI_ASM_HWCAP_H
+#define __UAPI_ASM_HWCAP_H
+
+/*
+ * Linux saves the floating-point registers according to the ISA Linux is
+ * executing on, as opposed to the ISA the user program is compiled for.  This
+ * is necessary for a handful of esoteric use cases: for example, userpsace
+ * threading libraries must be able to examine the actual machine state in
+ * order to fully reconstruct the state of a thread.
+ */
+#define COMPAT_HWCAP_ISA_I	(1 << ('I' - 'A'))
+#define COMPAT_HWCAP_ISA_M	(1 << ('M' - 'A'))
+#define COMPAT_HWCAP_ISA_A	(1 << ('A' - 'A'))
+#define COMPAT_HWCAP_ISA_F	(1 << ('F' - 'A'))
+#define COMPAT_HWCAP_ISA_D	(1 << ('D' - 'A'))
+#define COMPAT_HWCAP_ISA_C	(1 << ('C' - 'A'))
+
+#endif
arch/riscv/include/uapi/asm/ptrace.h new file +90 -0

90 lines not on this page · raw message

arch/riscv/include/uapi/asm/sigcontext.h new file +30 -0
new file mode 100644
index 000000000000..ed7372b277fa
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _UAPI_ASM_RISCV_SIGCONTEXT_H
+#define _UAPI_ASM_RISCV_SIGCONTEXT_H
+
+#include <asm/ptrace.h>
+
+/*
+ * Signal context structure
+ *
+ * This contains the context saved before a signal handler is invoked;
+ * it is restored by sys_sigreturn / sys_rt_sigreturn.
+ */
+struct sigcontext {
+	struct user_regs_struct sc_regs;
+	union __riscv_fp_state sc_fpregs;
+};
+
+#endif /* _UAPI_ASM_RISCV_SIGCONTEXT_H */
arch/riscv/include/uapi/asm/siginfo.h new file +24 -0
new file mode 100644
index 000000000000..f96849aac662
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2016 SiFive, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __ASM_SIGINFO_H
+#define __ASM_SIGINFO_H
+
+#define __ARCH_SI_PREAMBLE_SIZE	(__SIZEOF_POINTER__ == 4 ? 12 : 16)
+
+#include <asm-generic/siginfo.h>
+
+#endif
arch/riscv/include/uapi/asm/ucontext.h new file +35 -0
new file mode 100644
index 000000000000..52eff9febcfd
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2017 SiFive, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * This file was copied from arch/arm64/include/uapi/asm/ucontext.h
+ */
+#ifndef _UAPI__ASM_UCONTEXT_H
+#define _UAPI__ASM_UCONTEXT_H
+
+#include <linux/types.h>
+
+struct ucontext {
+	unsigned long	  uc_flags;
+	struct ucontext	 *uc_link;
+	stack_t		  uc_stack;
+	sigset_t	  uc_sigmask;
+	/* glibc uses a 1024-bit sigset_t */
+	__u8		  __unused[1024 / 8 - sizeof(sigset_t)];
+	/* last for future expansion */
+	struct sigcontext uc_mcontext;
+};
+
+#endif /* _UAPI__ASM_UCONTEXT_H */
arch/riscv/include/uapi/asm/unistd.h new file +23 -0
new file mode 100644
index 000000000000..37a5429cc896
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <asm-generic/unistd.h>
+
+/*
+ * These system calls add support for AMOs on RISC-V systems without support
+ * for the A extension.
+ */
+#define __NR_sysriscv_cmpxchg32		(__NR_arch_specific_syscall + 0)
+__SYSCALL(__NR_sysriscv_cmpxchg32, sys_sysriscv_cmpxchg32)
+#define __NR_sysriscv_cmpxchg64		(__NR_arch_specific_syscall + 1)
+__SYSCALL(__NR_sysriscv_cmpxchg64, sys_sysriscv_cmpxchg64)
arch/riscv/kernel/cpufeature.c new file +61 -0

61 lines not on this page · raw message

arch/riscv/kernel/module.c new file +217 -0

217 lines not on this page · raw message

arch/riscv/kernel/ptrace.c new file +138 -0

138 lines not on this page · raw message

arch/riscv/kernel/riscv_ksyms.c new file +15 -0
new file mode 100644
index 000000000000..23cc81ec9e94
@@ -0,0 +1,15 @@
+/*
+ * Copyright (C) 2017 Zihao Yu
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/export.h>
+#include <linux/uaccess.h>
+
+/*
+ * Assembly functions that may be used (directly or indirectly) by modules
+ */
+EXPORT_SYMBOL(__copy_user);
arch/riscv/kernel/signal.c new file +289 -0

289 lines not on this page · raw message

arch/riscv/kernel/sys_riscv.c new file +90 -0

90 lines not on this page · raw message

arch/riscv/kernel/syscall_table.c new file +25 -0
new file mode 100644
index 000000000000..8fa239b67cbc
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2009 Arnd Bergmann <[email protected]>
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/syscalls.h>
+
+#include <asm/syscalls.h>
+
+#undef __SYSCALL
+#define __SYSCALL(nr, call)	[nr] = (call),
+
+void *sys_call_table[__NR_syscalls] = {
+	[0 ... __NR_syscalls - 1] = sys_ni_syscall,
+#include <asm/unistd.h>
+};
arch/riscv/kernel/vdso/.gitignore new file +1 -0
new file mode 100644
index 000000000000..f8b69d84238e
@@ -0,0 +1 @@
+vdso.lds
arch/riscv/kernel/vdso/cmpxchg32.S new file +40 -0
new file mode 100644
index 000000000000..387deb4e852c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2017 SiFive
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/linkage.h>
+#include <asm/unistd.h>
+
+	.text
+ENTRY(__vdso_cmpxchg32)
+	.cfi_startproc
+#ifdef CONFIG_ISA_A
+/*
+ * a0: ptr
+ * a1: compare value
+ * a2: exchange value
+ */
+0:
+	lr.w.aqrl t0, 0(a0)
+	bne       t0, a1, 1f
+	sc.w.aqrl t1, a2, 0(a0)
+	bnez      t1, 0b
+1:
+	mv        a0, t0
+	ret
+#else
+	li a7, __NR_sysriscv_cmpxchg32
+	scall
+	ret
+#endif
+	.cfi_endproc
+ENDPROC(__vdso_cmpxchg32)
arch/riscv/kernel/vdso/cmpxchg64.S new file +40 -0
new file mode 100644
index 000000000000..6286842f78c0
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2017 SiFive
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/linkage.h>
+#include <asm/unistd.h>
+
+	.text
+ENTRY(__vdso_cmpxchg64)
+	.cfi_startproc
+#ifdef CONFIG_ISA_A
+/*
+ * a0: ptr
+ * a1: compare value
+ * a2: exchange value
+ */
+0:
+	lr.d.aqrl t0, 0(a0)
+	bne       t0, a1, 1f
+	sc.d.aqrl t1, a2, 0(a0)
+	bnez      t1, 0b
+1:
+	mv        a0, t0
+	ret
+#else
+	li a7, __NR_sysriscv_cmpxchg64
+	scall
+	ret
+#endif
+	.cfi_endproc
+ENDPROC(__vdso_cmpxchg64)
arch/riscv/kernel/vdso/sigreturn.S new file +24 -0
new file mode 100644
index 000000000000..f5aa3d72acfb
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2014 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/linkage.h>
+#include <asm/unistd.h>
+
+	.text
+ENTRY(__vdso_rt_sigreturn)
+	.cfi_startproc
+	.cfi_signal_frame
+	li a7, __NR_rt_sigreturn
+	scall
+	.cfi_endproc
+ENDPROC(__vdso_rt_sigreturn)
arch/riscv/kernel/vdso/vdso.S new file +27 -0
new file mode 100644
index 000000000000..7055de5f9174
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2014 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/init.h>
+#include <linux/linkage.h>
+#include <asm/page.h>
+
+	__PAGE_ALIGNED_DATA
+
+	.globl vdso_start, vdso_end
+	.balign PAGE_SIZE
+vdso_start:
+	.incbin "arch/riscv/kernel/vdso/vdso.so"
+	.balign PAGE_SIZE
+vdso_end:
+
+	.previous
Signature · 1 line
2.13.0
author maintainer
#44 lore raw

PATCH 9/9 RISC-V: Build Infastructure

In reply to Palmer Dabbelt

This patch contains all the build infastructure that actually enables the RISC-V port. This includes Makefiles, linker scripts, and Kconfig files. It also contains the only top-level change, which adds RISC-V to the list of architectures that need a sed run to produce the ARCH variable when building locally.

---
 Makefile                                  |   3 +-
 arch/riscv/Kconfig                        | 304 ++++++++++++++++++++++++++++++
 arch/riscv/Makefile                       |  64 +++++++
 arch/riscv/configs/freedom-u500_defconfig |  52 +++++
 arch/riscv/configs/spike32_defconfig      |  50 +++++
 arch/riscv/configs/spike64_defconfig      |  46 +++++
 arch/riscv/include/asm/Kbuild             |  61 ++++++
 arch/riscv/include/uapi/asm/Kbuild        |   4 +
 arch/riscv/kernel/.gitignore              |   1 +
 arch/riscv/kernel/Makefile                |  33 ++++
 arch/riscv/kernel/vdso/Makefile           |  61 ++++++
 arch/riscv/kernel/vdso/vdso.lds.S         |  77 ++++++++
 arch/riscv/kernel/vmlinux.lds.S           |  92 +++++++++
 arch/riscv/lib/Makefile                   |   6 +
 arch/riscv/mm/Makefile                    |   4 +
 15 files changed, 857 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/Kconfig
 create mode 100644 arch/riscv/Makefile
 create mode 100644 arch/riscv/configs/freedom-u500_defconfig
 create mode 100644 arch/riscv/configs/spike32_defconfig
 create mode 100644 arch/riscv/configs/spike64_defconfig
 create mode 100644 arch/riscv/include/asm/Kbuild
 create mode 100644 arch/riscv/include/uapi/asm/Kbuild
 create mode 100644 arch/riscv/kernel/.gitignore
 create mode 100644 arch/riscv/kernel/Makefile
 create mode 100644 arch/riscv/kernel/vdso/Makefile
 create mode 100644 arch/riscv/kernel/vdso/vdso.lds.S
 create mode 100644 arch/riscv/kernel/vmlinux.lds.S
 create mode 100644 arch/riscv/lib/Makefile
 create mode 100644 arch/riscv/mm/Makefile
Makefile +2 -1
index 283c6236438e..a7c77b6586b9 100644
@@ -232,7 +232,8 @@ SUBARCH := $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ \
 				  -e s/arm.*/arm/ -e s/sa110/arm/ \
 				  -e s/s390x/s390/ -e s/parisc64/parisc/ \
 				  -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
-				  -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ )
+				  -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ \
+				  -e s/riscv.*/riscv/)
 
 # Cross compiling and selecting different set of gcc/bin-utils
 # ---------------------------------------------------------------------------
arch/riscv/Kconfig new file +304 -0

304 lines not on this page · raw message

arch/riscv/Makefile new file +64 -0

64 lines not on this page · raw message

arch/riscv/configs/freedom-u500_defconfig new file +52 -0

52 lines not on this page · raw message

arch/riscv/configs/spike32_defconfig new file +50 -0

50 lines not on this page · raw message

arch/riscv/configs/spike64_defconfig new file +46 -0

46 lines not on this page · raw message

arch/riscv/include/asm/Kbuild new file +61 -0

61 lines not on this page · raw message

arch/riscv/include/uapi/asm/Kbuild new file +4 -0
new file mode 100644
index 000000000000..0727570a49b2
@@ -0,0 +1,4 @@
+# UAPI Header export list
+include include/uapi/asm-generic/Kbuild.asm
+
+generic-y += setup.h
arch/riscv/kernel/.gitignore new file +1 -0
new file mode 100644
index 000000000000..b51634f6a7cd
@@ -0,0 +1 @@
+/vmlinux.lds
arch/riscv/kernel/Makefile new file +33 -0
new file mode 100644
index 000000000000..ab8baf7bd142
@@ -0,0 +1,33 @@
+#
+# Makefile for the RISC-V Linux kernel
+#
+
+extra-y += head.o
+extra-y += vmlinux.lds
+
+obj-y	+= cpu.o
+obj-y	+= cpufeature.o
+obj-y	+= entry.o
+obj-y	+= irq.o
+obj-y	+= process.o
+obj-y	+= ptrace.o
+obj-y	+= reset.o
+obj-y	+= setup.o
+obj-y	+= signal.o
+obj-y	+= syscall_table.o
+obj-y	+= sys_riscv.o
+obj-y	+= time.o
+obj-y	+= traps.o
+obj-y	+= riscv_ksyms.o
+obj-y	+= stacktrace.o
+obj-y	+= vdso.o
+obj-y	+= cacheinfo.o
+obj-y	+= vdso/
+
+CFLAGS_setup.o := -mcmodel=medany
+
+obj-$(CONFIG_SMP)		+= smpboot.o
+obj-$(CONFIG_SMP)		+= smp.o
+obj-$(CONFIG_MODULES)		+= module.o
+
+clean:
arch/riscv/kernel/vdso/Makefile new file +61 -0

61 lines not on this page · raw message

arch/riscv/kernel/vdso/vdso.lds.S new file +77 -0

77 lines not on this page · raw message

arch/riscv/kernel/vmlinux.lds.S new file +92 -0

92 lines not on this page · raw message

arch/riscv/lib/Makefile new file +6 -0
new file mode 100644
index 000000000000..596c2ca40d63
@@ -0,0 +1,6 @@
+lib-y	+= delay.o
+lib-y	+= memcpy.o
+lib-y	+= memset.o
+lib-y	+= uaccess.o
+
+lib-$(CONFIG_32BIT) += udivdi3.o
arch/riscv/mm/Makefile new file +4 -0
new file mode 100644
index 000000000000..81f7d9ce6d88
@@ -0,0 +1,4 @@
+obj-y += init.o
+obj-y += fault.o
+obj-y += extable.o
+obj-y += ioremap.o
Signature · 1 line
2.13.0

Palmer Dabbelt

author maintainer
#0 lore raw

Thanks to everyone who has participated in the review process so far. There have only been a few changes since the v3 patch set:

* The cmpxchg64 syscall is no longer enabled on 32-bit systems. It's not possible to provide this on SMP systems, and it's not necessary as glibc knows not to call it.

* We provide a ELF_HWCAP so users can determine the ISA of the machine the kernel is running on.

* The multi-line comments are in a better form.

* There were a handful of headers that could be replaced with the asm-generic versions, and a few unnecessary definitions.

* We no longer use printk, but instead use pr_*.

* A few Kconfig and defconfig entries have been cleaned up.

Since things have really calmed down, I think it would be good to try and get this into 4.13 (ie, during this merge window). This might let us get into the upcoming glibc release as well. Does that seem possible?

Like last time, in case one gets eaten by the mailing list this is also available as a git tree on our Git Hub

https://github.com/riscv/riscv-linux/tree/riscv-for-submission-arch-v4

This patch set just contains the arch code, we have various drivers that are required to build and boot a RISC-V system. A tree that contains this patch set merged with all our other patch sets lives at

https://github.com/riscv/riscv-linux/tree/riscv-for-submission-v4

a83d4bb70b76 pci: Add a generic, weakly-linked pcibios_fixup_bus a3445d3b4ff1 pci: Add a generic, weakly-linked pcibios_align_resource d97ac4f5bb1d Merge branch 'riscv-for-submission-arch-v4' into riscv-for-submission-v4 0705a31372d4 Merge branch 'riscv-for-submission-clk-v2' into riscv-for-submission-v4 a6c8eb9af199 Merge branch 'riscv-for-submission-irq-v2' into riscv-for-submission-v4 3e5b46967f66 Merge branch 'riscv-for-submission-lib-v2' into riscv-for-submission-v4 8ca3c2cdefed Merge branch 'riscv-for-submission-pci-v3' into riscv-for-submission-v4 e1aa9abddfa8 Merge branch 'riscv-for-submission-tty-v2' into riscv-for-submission-v4

If you're going to try to build or boot the kernel, I'd recommend using that.

Thanks to everyone who has helped review our port!

[PATCH 1/9] RISC-V: Init and Halt Code
[PATCH 2/9] RISC-V: Atomic and Locking Code [PATCH 3/9] RISC-V: Generic library routines and assembly [PATCH 4/9] RISC-V: ELF and module implementation [PATCH 5/9] RISC-V: Task implementation
[PATCH 6/9] RISC-V: Device, timer, IRQs, and the SBI [PATCH 7/9] RISC-V: Paging and MMU
[PATCH 8/9] RISC-V: User-facing API
[PATCH 9/9] RISC-V: Build Infastructure

author maintainer
#9 lore raw

PATCH 1/9 RISC-V: Init and Halt Code

This contains the various __init C functions, the initial assembly kernel entry point, and the code to reset the system. When a file was init-related, it contains

---
 arch/riscv/include/asm/bug.h   |  88 +++++++++++++++
 arch/riscv/include/asm/cache.h |  22 ++++
 arch/riscv/include/asm/smp.h   |  41 +++++++
 arch/riscv/kernel/cacheinfo.c  | 105 ++++++++++++++++++
 arch/riscv/kernel/cpu.c        |  89 +++++++++++++++
 arch/riscv/kernel/head.S       | 152 +++++++++++++++++++++++++
 arch/riscv/kernel/irq.c        |  20 ++++
 arch/riscv/kernel/reset.c      |  36 ++++++
 arch/riscv/kernel/setup.c      | 246 +++++++++++++++++++++++++++++++++++++++++
 arch/riscv/kernel/smp.c        | 110 ++++++++++++++++++
 arch/riscv/kernel/smpboot.c    | 114 +++++++++++++++++++
 arch/riscv/kernel/time.c       |  61 ++++++++++
 arch/riscv/kernel/traps.c      | 184 ++++++++++++++++++++++++++++++
 arch/riscv/kernel/vdso.c       | 125 +++++++++++++++++++++
 arch/riscv/mm/init.c           |  70 ++++++++++++
 15 files changed, 1463 insertions(+)
 create mode 100644 arch/riscv/include/asm/bug.h
 create mode 100644 arch/riscv/include/asm/cache.h
 create mode 100644 arch/riscv/include/asm/smp.h
 create mode 100644 arch/riscv/kernel/cacheinfo.c
 create mode 100644 arch/riscv/kernel/cpu.c
 create mode 100644 arch/riscv/kernel/head.S
 create mode 100644 arch/riscv/kernel/irq.c
 create mode 100644 arch/riscv/kernel/reset.c
 create mode 100644 arch/riscv/kernel/setup.c
 create mode 100644 arch/riscv/kernel/smp.c
 create mode 100644 arch/riscv/kernel/smpboot.c
 create mode 100644 arch/riscv/kernel/time.c
 create mode 100644 arch/riscv/kernel/traps.c
 create mode 100644 arch/riscv/kernel/vdso.c
 create mode 100644 arch/riscv/mm/init.c
arch/riscv/include/asm/bug.h new file +88 -0
new file mode 100644
index 000000000000..e2f690c20729
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_BUG_H
+#define _ASM_RISCV_BUG_H
+
+#include <linux/compiler.h>
+#include <linux/const.h>
+#include <linux/types.h>
+
+#include <asm/asm.h>
+
+#ifdef CONFIG_GENERIC_BUG
+#define __BUG_INSN	_AC(0x00100073, UL) /* sbreak */
+
+#ifndef __ASSEMBLY__
+typedef u32 bug_insn_t;
+
+#ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
+#define __BUG_ENTRY_ADDR	INT " 1b - 2b"
+#define __BUG_ENTRY_FILE	INT " %0 - 2b"
+#else
+#define __BUG_ENTRY_ADDR	RISCV_PTR " 1b"
+#define __BUG_ENTRY_FILE	RISCV_PTR " %0"
+#endif
+
+#ifdef CONFIG_DEBUG_BUGVERBOSE
+#define __BUG_ENTRY			\
+	__BUG_ENTRY_ADDR "\n\t"		\
+	__BUG_ENTRY_FILE "\n\t"		\
+	SHORT " %1"
+#else
+#define __BUG_ENTRY			\
+	__BUG_ENTRY_ADDR
+#endif
+
+#define BUG()							\
+do {								\
+	__asm__ __volatile__ (					\
+		"1:\n\t"					\
+			"sbreak\n"				\
+			".pushsection __bug_table,\"a\"\n\t"	\
+		"2:\n\t"					\
+			__BUG_ENTRY "\n\t"			\
+			".org 2b + %2\n\t"			\
+			".popsection"				\
+		:						\
+		: "i" (__FILE__), "i" (__LINE__),		\
+		  "i" (sizeof(struct bug_entry)));		\
+	unreachable();						\
+} while (0)
+#endif /* !__ASSEMBLY__ */
+#else /* CONFIG_GENERIC_BUG */
+#ifndef __ASSEMBLY__
+#define BUG()							\
+do {								\
+	__asm__ __volatile__ ("sbreak\n");			\
+	unreachable();						\
+} while (0)
+#endif /* !__ASSEMBLY__ */
+#endif /* CONFIG_GENERIC_BUG */
+
+#define HAVE_ARCH_BUG
+
+#include <asm-generic/bug.h>
+
+#ifndef __ASSEMBLY__
+
+struct pt_regs;
+struct task_struct;
+
+extern void die(struct pt_regs *regs, const char *str);
+extern void do_trap(struct pt_regs *regs, int signo, int code,
+	unsigned long addr, struct task_struct *tsk);
+
+#endif /* !__ASSEMBLY__ */
+
+#endif /* _ASM_RISCV_BUG_H */
arch/riscv/include/asm/cache.h new file +22 -0
new file mode 100644
index 000000000000..e8f0d1110d74
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2017 Chen Liqin <[email protected]>
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_CACHE_H
+#define _ASM_RISCV_CACHE_H
+
+#define L1_CACHE_SHIFT		6
+
+#define L1_CACHE_BYTES		(1 << L1_CACHE_SHIFT)
+
+#endif /* _ASM_RISCV_CACHE_H */
arch/riscv/include/asm/smp.h new file +41 -0

41 lines not on this page · raw message

arch/riscv/kernel/cacheinfo.c new file +105 -0

105 lines not on this page · raw message

arch/riscv/kernel/cpu.c new file +89 -0

89 lines not on this page · raw message

arch/riscv/kernel/head.S new file +152 -0

152 lines not on this page · raw message

arch/riscv/kernel/irq.c new file +20 -0
new file mode 100644
index 000000000000..737d7cce2c6d
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2017 SiFive
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/irqchip.h>
+
+void __init init_IRQ(void)
+{
+	irqchip_init();
+}
arch/riscv/kernel/reset.c new file +36 -0
new file mode 100644
index 000000000000..2a53d26ffdd6
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/reboot.h>
+#include <linux/export.h>
+#include <asm/sbi.h>
+
+void (*pm_power_off)(void) = machine_power_off;
+EXPORT_SYMBOL(pm_power_off);
+
+void machine_restart(char *cmd)
+{
+	do_kernel_restart(cmd);
+	while (1);
+}
+
+void machine_halt(void)
+{
+	machine_power_off();
+}
+
+void machine_power_off(void)
+{
+	sbi_shutdown();
+	while (1);
+}
arch/riscv/kernel/setup.c new file +246 -0

246 lines not on this page · raw message

arch/riscv/kernel/smp.c new file +110 -0

110 lines not on this page · raw message

arch/riscv/kernel/smpboot.c new file +114 -0

114 lines not on this page · raw message

arch/riscv/kernel/time.c new file +61 -0

61 lines not on this page · raw message

arch/riscv/kernel/traps.c new file +184 -0

184 lines not on this page · raw message

arch/riscv/kernel/vdso.c new file +125 -0

125 lines not on this page · raw message

arch/riscv/mm/init.c new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#22 lore raw

PATCH 2/9 RISC-V: Atomic and Locking Code

In reply to Palmer Dabbelt

This contains all the code that directly interfaces with the RISC-V memory model. While this code corforms to the current RISC-V ISA specifications (user 2.2 and priv 1.10), the memory model is somewhat underspecified in those documents. There is a working group that hopes to produce a formal memory model by the end of the year, but my understanding is that the basic definitions we're relying on here won't change significantly.

---
 arch/riscv/include/asm/atomic.h         | 337 ++++++++++++++++++++++++++++++++
 arch/riscv/include/asm/barrier.h        | 143 ++++++++++++++
 arch/riscv/include/asm/bitops.h         | 228 +++++++++++++++++++++
 arch/riscv/include/asm/cacheflush.h     |  39 ++++
 arch/riscv/include/asm/cmpxchg.h        | 138 +++++++++++++
 arch/riscv/include/asm/io.h             | 180 +++++++++++++++++
 arch/riscv/include/asm/spinlock.h       | 167 ++++++++++++++++
 arch/riscv/include/asm/spinlock_types.h |  33 ++++
 arch/riscv/include/asm/tlb.h            |  24 +++
 arch/riscv/include/asm/tlbflush.h       |  64 ++++++
 10 files changed, 1353 insertions(+)
 create mode 100644 arch/riscv/include/asm/atomic.h
 create mode 100644 arch/riscv/include/asm/barrier.h
 create mode 100644 arch/riscv/include/asm/bitops.h
 create mode 100644 arch/riscv/include/asm/cacheflush.h
 create mode 100644 arch/riscv/include/asm/cmpxchg.h
 create mode 100644 arch/riscv/include/asm/io.h
 create mode 100644 arch/riscv/include/asm/spinlock.h
 create mode 100644 arch/riscv/include/asm/spinlock_types.h
 create mode 100644 arch/riscv/include/asm/tlb.h
 create mode 100644 arch/riscv/include/asm/tlbflush.h
arch/riscv/include/asm/atomic.h new file +337 -0
new file mode 100644
index 000000000000..e5a12cfa405a
@@ -0,0 +1,337 @@
+/*
+ * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2017 SiFive
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public Licence
+ * as published by the Free Software Foundation; either version
+ * 2 of the Licence, or (at your option) any later version.
+ */
+
+#ifndef _ASM_RISCV_ATOMIC_H
+#define _ASM_RISCV_ATOMIC_H
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+# include <asm-generic/atomic64.h>
+#else
+# if (__riscv_xlen < 64)
+#  error "64-bit atomics require XLEN to be at least 64"
+# endif
+#endif
+
+#ifdef CONFIG_ISA_A
+
+#include <asm/cmpxchg.h>
+#include <asm/barrier.h>
+
+#define ATOMIC_INIT(i)	{ (i) }
+static __always_inline int atomic_read(const atomic_t *v)
+{
+	return READ_ONCE(v->counter);
+}
+static __always_inline void atomic_set(atomic_t *v, int i)
+{
+	WRITE_ONCE(v->counter, i);
+}
+
+#ifndef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC64_INIT(i) { (i) }
+static __always_inline int atomic64_read(const atomic64_t *v)
+{
+	return READ_ONCE(v->counter);
+}
+static __always_inline void atomic64_set(atomic64_t *v, int i)
+{
+	WRITE_ONCE(v->counter, i);
+}
+#endif
+
+/*
+ * First, the atomic ops that have no ordering constraints and therefor don't
+ * have the AQ or RL bits set.  These don't return anything, so there's only
+ * one version to worry about.
+ */
+#define ATOMIC_OP(op, asm_op, c_op, I, asm_type, c_type, prefix)				\
+static __always_inline void atomic##prefix##_##op(c_type i, atomic##prefix##_t *v)		\
+{												\
+	__asm__ __volatile__ (									\
+		"amo" #asm_op "." #asm_type " zero, %1, %0"					\
+		: "+A" (v->counter)								\
+		: "r" (I));									\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, asm_op, c_op, I)			\
+        ATOMIC_OP (op, asm_op, c_op, I, w,  int,   )
+#else
+#define ATOMIC_OPS(op, asm_op, c_op, I)			\
+        ATOMIC_OP (op, asm_op, c_op, I, w,  int,   )	\
+        ATOMIC_OP (op, asm_op, c_op, I, d, long, 64)
+#endif
+
+ATOMIC_OPS(add, add, +,  i)
+ATOMIC_OPS(sub, add, +, -i)
+/*
+ * FIXME: I could only find documentation that atomic_{add,sub,inc,dec} are
+ * barrier-free.  I'm assuming that and/or/xor have the same constraints as the
+ * others.
+ */
+ATOMIC_OPS(and, and, &,  i)
+ATOMIC_OPS( or,  or, |,  i)
+ATOMIC_OPS(xor, xor, ^,  i)
+
+#undef ATOMIC_OP
+#undef ATOMIC_OPS
+
+/*
+ * Atomic ops that have ordered, relaxed, acquire, and relese variants.
+ * There's two flavors of these: the arithmatic ops have both fetch and return
+ * versions, while the logical ops only have fetch versions.
+ */
+#define ATOMIC_FETCH_OP(op, asm_op, c_op, I, asm_or, c_or, asm_type, c_type, prefix)			\
+static __always_inline c_type atomic##prefix##_fetch_##op##c_or(c_type i, atomic##prefix##_t *v)	\
+{													\
+	register c_type ret;										\
+	__asm__ __volatile__ (										\
+		"amo" #asm_op "." #asm_type #asm_or " %2, %1, %0"					\
+		: "+A" (v->counter), "=r" (ret)								\
+		: "r" (I));										\
+	return ret;											\
+}
+
+#define ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_or, c_or, asm_type, c_type, prefix)			\
+static __always_inline c_type atomic##prefix##_##op##_return##c_or(int i, atomic##prefix##_t *v)	\
+{													\
+        return atomic##prefix##_fetch_##op##c_or(i, v) c_op I;						\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, asm_op, c_op, I, asm_or, c_or)				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I, asm_or, c_or, w,  int,   )	\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_or, c_or, w,  int,   )
+#else
+#define ATOMIC_OPS(op, asm_op, c_op, I, asm_or, c_or)				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I, asm_or, c_or, w,  int,   )	\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_or, c_or, w,  int,   )	\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I, asm_or, c_or, d, long, 64)	\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I, asm_or, c_or, d, long, 64)
+#endif
+
+ATOMIC_OPS(add, add, +,  i,      , _relaxed)
+ATOMIC_OPS(add, add, +,  i, .aq  , _acquire)
+ATOMIC_OPS(add, add, +,  i, .rl  , _release)
+ATOMIC_OPS(add, add, +,  i, .aqrl,         )
+
+ATOMIC_OPS(sub, add, +, -i,      , _relaxed)
+ATOMIC_OPS(sub, add, +, -i, .aq  , _acquire)
+ATOMIC_OPS(sub, add, +, -i, .rl  , _release)
+ATOMIC_OPS(sub, add, +, -i, .aqrl,         )
+
+#undef ATOMIC_OPS
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, asm_op, c_op, I, asm_or, c_or)				\
+        ATOMIC_FETCH_OP(op, asm_op, c_op, I, asm_or, c_or, w,  int,   )
+#else
+#define ATOMIC_OPS(op, asm_op, c_op, I, asm_or, c_or)				\
+        ATOMIC_FETCH_OP(op, asm_op, c_op, I, asm_or, c_or, w,  int,   )		\
+        ATOMIC_FETCH_OP(op, asm_op, c_op, I, asm_or, c_or, d, long, 64)
+#endif
+
+ATOMIC_OPS(and, and, &,  i,      , _relaxed)
+ATOMIC_OPS(and, and, &,  i, .aq  , _acquire)
+ATOMIC_OPS(and, and, &,  i, .rl  , _release)
+ATOMIC_OPS(and, and, &,  i, .aqrl,         )
+
+ATOMIC_OPS( or,  or, |,  i,      , _relaxed)
+ATOMIC_OPS( or,  or, |,  i, .aq  , _acquire)
+ATOMIC_OPS( or,  or, |,  i, .rl  , _release)
+ATOMIC_OPS( or,  or, |,  i, .aqrl,         )
+
+ATOMIC_OPS(xor, xor, ^,  i,      , _relaxed)
+ATOMIC_OPS(xor, xor, ^,  i, .aq  , _acquire)
+ATOMIC_OPS(xor, xor, ^,  i, .rl  , _release)
+ATOMIC_OPS(xor, xor, ^,  i, .aqrl,         )
+
+#undef ATOMIC_OPS
+
+#undef ATOMIC_FETCH_OP
+#undef ATOMIC_OP_RETURN
+
+/*
+ * The extra atomic operations that are constructed from one of the core
+ * AMO-based operations above (aside from sub, which is easier to fit above).
+ * These are required to perform a barrier, but they're OK this way because
+ * atomic_*_return is also required to perform a barrier.
+ */
+#define ATOMIC_OP(op, func_op, comp_op, I, c_type, prefix)			\
+static __always_inline bool atomic##prefix##_##op(c_type i, atomic##prefix##_t *v) \
+{										\
+	return atomic##prefix##_##func_op##_return(i, v) comp_op I;		\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, func_op, comp_op, I)			\
+        ATOMIC_OP (op, func_op, comp_op, I,  int,   )
+#else
+#define ATOMIC_OPS(op, func_op, comp_op, I)			\
+        ATOMIC_OP (op, func_op, comp_op, I,  int,   )		\
+        ATOMIC_OP (op, func_op, comp_op, I, long, 64)
+#endif
+
+ATOMIC_OPS(add_and_test, add, ==, 0)
+ATOMIC_OPS(sub_and_test, sub, ==, 0)
+ATOMIC_OPS(add_negative, add,  <, 0)
+
+#undef ATOMIC_OP
+#undef ATOMIC_OPS
+
+#define ATOMIC_OP(op, func_op, c_op, I, prefix)					\
+static __always_inline void atomic##prefix##_##op(atomic##prefix##_t *v)	\
+{										\
+	atomic##prefix##_##func_op(I, v);					\
+}
+
+#define ATOMIC_FETCH_OP(op, func_op, c_op, I, prefix)				\
+static __always_inline int atomic##prefix##_fetch_##op(atomic##prefix##_t *v)	\
+{										\
+	return atomic##prefix##_fetch_##func_op(I, v);				\
+}
+
+#define ATOMIC_OP_RETURN(op, asm_op, c_op, I, prefix)				\
+static __always_inline int atomic##prefix##_##op##_return(atomic##prefix##_t *v) \
+{										\
+        return atomic##prefix##_fetch_##op(v) c_op I;				\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(op, asm_op, c_op, I)						\
+        ATOMIC_OP       (op, asm_op, c_op, I,   )				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I,   )				\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I,   )
+#else
+#define ATOMIC_OPS(op, asm_op, c_op, I)						\
+        ATOMIC_OP       (op, asm_op, c_op, I,   )				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I,   )				\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I,   )				\
+        ATOMIC_OP       (op, asm_op, c_op, I, 64)				\
+        ATOMIC_FETCH_OP (op, asm_op, c_op, I, 64)				\
+        ATOMIC_OP_RETURN(op, asm_op, c_op, I, 64)
+#endif
+
+ATOMIC_OPS(inc, add, +,  1)
+ATOMIC_OPS(dec, add, +, -1)
+
+#undef ATOMIC_OPS
+#undef ATOMIC_OP
+#undef ATOMIC_FETCH_OP
+#undef ATOMIC_OP_RETURN
+
+#define ATOMIC_OP(op, func_op, comp_op, I, prefix)				\
+static __always_inline bool atomic##prefix##_##op(atomic##prefix##_t *v)	\
+{										\
+	return atomic##prefix##_##func_op##_return(v) comp_op I;		\
+}
+
+ATOMIC_OP(inc_and_test, inc, ==, 0,   )
+ATOMIC_OP(dec_and_test, dec, ==, 0,   )
+#ifndef CONFIG_GENERIC_ATOMIC64
+ATOMIC_OP(inc_and_test, inc, ==, 0, 64)
+ATOMIC_OP(dec_and_test, dec, ==, 0, 64)
+#endif
+
+#undef ATOMIC_OP
+
+/* This is required to provide a barrier on success. */
+static __always_inline int __atomic_add_unless(atomic_t *v, int a, int u)
+{
+       register int prev, rc;
+
+	__asm__ __volatile__ (
+		"0:\n\t"
+		"lr.w.aqrl %0, %2\n\t"
+		"beq       %0, %4, 1f\n\t"
+		"add       %1, %0, %3\n\t"
+		"sc.w.aqrl %1, %1, %2\n\t"
+		"bnez      %1, 0b\n\t"
+		"1:"
+		: "=&r" (prev), "=&r" (rc), "+A" (v->counter)
+		: "r" (a), "r" (u));
+	return prev;
+}
+
+#ifndef CONFIG_GENERIC_ATOMIC64
+static __always_inline long atomic64_add_unless(atomic64_t *v, long a, long u)
+{
+       register long prev, rc;
+
+	__asm__ __volatile__ (
+		"0:\n\t"
+		"lr.d.aqrl %0, %2\n\t"
+		"beq       %0, %4, 1f\n\t"
+		"add       %1, %0, %3\n\t"
+		"sc.d.aqrl %1, %1, %2\n\t"
+		"bnez      %1, 0b\n\t"
+		"1:"
+		: "=&r" (prev), "=&r" (rc), "+A" (v->counter)
+		: "r" (a), "r" (u));
+	return prev;
+}
+#endif
+
+/*
+ * The extra atomic operations that are constructed from one of the core
+ * LR/SC-based operations above.
+ */
+static __always_inline int atomic_inc_not_zero(atomic_t *v)
+{
+        return __atomic_add_unless(v, 1, 0);
+}
+
+#ifndef CONFIG_GENERIC_ATOMIC64
+static __always_inline int atomic64_inc_not_zero(atomic64_t *v)
+{
+        return atomic64_add_unless(v, 1, 0);
+}
+#endif
+
+/*
+ * atomic_{cmp,}xchg is required to have exactly the same ordering semantics as
+ * {cmp,}xchg and the operations that return, so they need a barrier.  We just
+ * use the other implementations directly.
+ */
+#define ATOMIC_OP(c_t, prefix, c_or, size, asm_or)						\
+static __always_inline c_t atomic##prefix##_cmpxchg##c_or(atomic##prefix##_t *v, c_t o, c_t n) 	\
+{												\
+	return __cmpxchg(&(v->counter), o, n, size, asm_or, asm_or);				\
+}												\
+static __always_inline c_t atomic##prefix##_xchg##c_or(atomic##prefix##_t *v, c_t n) 		\
+{												\
+	return __xchg(n, &(v->counter), size, asm_or);						\
+}
+
+#ifdef CONFIG_GENERIC_ATOMIC64
+#define ATOMIC_OPS(c_or, asm_or)			\
+	ATOMIC_OP( int,   , c_or, 4, asm_or)
+#else
+#define ATOMIC_OPS(c_or, asm_or)			\
+	ATOMIC_OP( int,   , c_or, 4, asm_or)		\
+	ATOMIC_OP(long, 64, c_or, 8, asm_or)
+#endif
+
+ATOMIC_OPS(        , .aqrl)
+ATOMIC_OPS(_acquire,   .aq)
+ATOMIC_OPS(_release,   .rl)
+ATOMIC_OPS(_relaxed,      )
+
+#undef ATOMIC_OPS
+#undef ATOMIC_OP
+
+#else /* !CONFIG_ISA_A */
+
+#include <asm-generic/atomic.h>
+
+#endif /* CONFIG_ISA_A */
+
+#endif /* _ASM_RISCV_ATOMIC_H */
arch/riscv/include/asm/barrier.h new file +143 -0

143 lines not on this page · raw message

arch/riscv/include/asm/bitops.h new file +228 -0

228 lines not on this page · raw message

arch/riscv/include/asm/cacheflush.h new file +39 -0
new file mode 100644
index 000000000000..0595585013b0
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_CACHEFLUSH_H
+#define _ASM_RISCV_CACHEFLUSH_H
+
+#include <asm-generic/cacheflush.h>
+
+#undef flush_icache_range
+#undef flush_icache_user_range
+
+static inline void local_flush_icache_all(void)
+{
+	asm volatile ("fence.i" ::: "memory");
+}
+
+#ifndef CONFIG_SMP
+
+#define flush_icache_range(start, end) local_flush_icache_all()
+#define flush_icache_user_range(vma, pg, addr, len) local_flush_icache_all()
+
+#else /* CONFIG_SMP */
+
+#define flush_icache_range(start, end) sbi_remote_fence_i(0)
+#define flush_icache_user_range(vma, pg, addr, len) sbi_remote_fence_i(0)
+
+#endif /* CONFIG_SMP */
+
+#endif /* _ASM_RISCV_CACHEFLUSH_H */
arch/riscv/include/asm/cmpxchg.h new file +138 -0

138 lines not on this page · raw message

arch/riscv/include/asm/io.h new file +180 -0

180 lines not on this page · raw message

arch/riscv/include/asm/spinlock.h new file +167 -0

167 lines not on this page · raw message

arch/riscv/include/asm/spinlock_types.h new file +33 -0
new file mode 100644
index 000000000000..83ac4ac9e2ac
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_SPINLOCK_TYPES_H
+#define _ASM_RISCV_SPINLOCK_TYPES_H
+
+#ifndef __LINUX_SPINLOCK_TYPES_H
+# error "please don't include this file directly"
+#endif
+
+typedef struct {
+	volatile unsigned int lock;
+} arch_spinlock_t;
+
+#define __ARCH_SPIN_LOCK_UNLOCKED	{ 0 }
+
+typedef struct {
+	volatile unsigned int lock;
+} arch_rwlock_t;
+
+#define __ARCH_RW_LOCK_UNLOCKED		{ 0 }
+
+#endif
arch/riscv/include/asm/tlb.h new file +24 -0
new file mode 100644
index 000000000000..c229509288ea
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_TLB_H
+#define _ASM_RISCV_TLB_H
+
+#include <asm-generic/tlb.h>
+
+static inline void tlb_flush(struct mmu_gather *tlb)
+{
+	flush_tlb_mm(tlb->mm);
+}
+
+#endif /* _ASM_RISCV_TLB_H */
arch/riscv/include/asm/tlbflush.h new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#26 lore raw

PATCH 5/9 RISC-V: Task implementation

In reply to Palmer Dabbelt

This patch contains the implementation of tasks on RISC-V, most of which is involved in task switching.

---
 arch/riscv/include/asm/asm-offsets.h |   1 +
 arch/riscv/include/asm/current.h     |  44 ++++
 arch/riscv/include/asm/kprobes.h     |  22 ++
 arch/riscv/include/asm/processor.h   |  97 ++++++++
 arch/riscv/include/asm/switch_to.h   |  69 ++++++
 arch/riscv/include/asm/thread_info.h |  93 ++++++++
 arch/riscv/kernel/asm-offsets.c      | 318 +++++++++++++++++++++++++
 arch/riscv/kernel/entry.S            | 450 +++++++++++++++++++++++++++++++++++
 arch/riscv/kernel/process.c          | 132 ++++++++++
 9 files changed, 1226 insertions(+)
 create mode 100644 arch/riscv/include/asm/asm-offsets.h
 create mode 100644 arch/riscv/include/asm/current.h
 create mode 100644 arch/riscv/include/asm/kprobes.h
 create mode 100644 arch/riscv/include/asm/processor.h
 create mode 100644 arch/riscv/include/asm/switch_to.h
 create mode 100644 arch/riscv/include/asm/thread_info.h
 create mode 100644 arch/riscv/kernel/asm-offsets.c
 create mode 100644 arch/riscv/kernel/entry.S
 create mode 100644 arch/riscv/kernel/process.c
arch/riscv/include/asm/asm-offsets.h new file +1 -0
new file mode 100644
index 000000000000..d370ee36a182
@@ -0,0 +1 @@
+#include <generated/asm-offsets.h>
arch/riscv/include/asm/current.h new file +44 -0

44 lines not on this page · raw message

arch/riscv/include/asm/kprobes.h new file +22 -0
new file mode 100644
index 000000000000..c7eb010d1528
@@ -0,0 +1,22 @@
+/*
+ * Copied from arch/arm64/include/asm/kprobes.h
+ *
+ * Copyright (C) 2013 Linaro Limited
+ * Copyright (C) 2017 SiFive
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#ifndef _RISCV_KPROBES_H
+#define _RISCV_KPROBES_H
+
+#include <asm-generic/kprobes.h>
+
+#endif /* _RISCV_KPROBES_H */
arch/riscv/include/asm/processor.h new file +97 -0

97 lines not on this page · raw message

arch/riscv/include/asm/switch_to.h new file +69 -0

69 lines not on this page · raw message

arch/riscv/include/asm/thread_info.h new file +93 -0

93 lines not on this page · raw message

arch/riscv/kernel/asm-offsets.c new file +318 -0

318 lines not on this page · raw message

arch/riscv/kernel/entry.S new file +450 -0

450 lines not on this page · raw message

arch/riscv/kernel/process.c new file +132 -0

132 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#23 lore raw

PATCH 3/9 RISC-V: Generic library routines and assembly

In reply to Palmer Dabbelt

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.

---
 arch/riscv/include/asm/asm.h            |  76 ++++++
 arch/riscv/include/asm/bitops.h         |   4 -
 arch/riscv/include/asm/csr.h            | 125 +++++++++
 arch/riscv/include/asm/linkage.h        |  20 ++
 arch/riscv/include/asm/string.h         |  26 ++
 arch/riscv/include/asm/uaccess.h        | 441 ++++++++++++++++++++++++++++++++
 arch/riscv/include/asm/word-at-a-time.h |  55 ++++
 arch/riscv/kernel/stacktrace.c          | 177 +++++++++++++
 arch/riscv/lib/memcpy.S                 | 100 ++++++++
 arch/riscv/lib/memset.S                 | 120 +++++++++
 arch/riscv/lib/uaccess.S                | 127 +++++++++
 arch/riscv/lib/udivdi3.S                |  38 +++
 12 files changed, 1305 insertions(+), 4 deletions(-)
 create mode 100644 arch/riscv/include/asm/asm.h
 create mode 100644 arch/riscv/include/asm/csr.h
 create mode 100644 arch/riscv/include/asm/linkage.h
 create mode 100644 arch/riscv/include/asm/string.h
 create mode 100644 arch/riscv/include/asm/uaccess.h
 create mode 100644 arch/riscv/include/asm/word-at-a-time.h
 create mode 100644 arch/riscv/kernel/stacktrace.c
 create mode 100644 arch/riscv/lib/memcpy.S
 create mode 100644 arch/riscv/lib/memset.S
 create mode 100644 arch/riscv/lib/uaccess.S
 create mode 100644 arch/riscv/lib/udivdi3.S
arch/riscv/include/asm/asm.h new file +76 -0
new file mode 100644
index 000000000000..6cbbb6a68d76
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_ASM_H
+#define _ASM_RISCV_ASM_H
+
+#ifdef __ASSEMBLY__
+#define __ASM_STR(x)	x
+#else
+#define __ASM_STR(x)	#x
+#endif
+
+#if __riscv_xlen == 64
+#define __REG_SEL(a, b)	__ASM_STR(a)
+#elif __riscv_xlen == 32
+#define __REG_SEL(a, b)	__ASM_STR(b)
+#else
+#error "Unexpected __riscv_xlen"
+#endif
+
+#define REG_L		__REG_SEL(ld, lw)
+#define REG_S		__REG_SEL(sd, sw)
+#define SZREG		__REG_SEL(8, 4)
+#define LGREG		__REG_SEL(3, 2)
+
+#if __SIZEOF_POINTER__ == 8
+#ifdef __ASSEMBLY__
+#define RISCV_PTR		.dword
+#define RISCV_SZPTR		8
+#define RISCV_LGPTR		3
+#else
+#define RISCV_PTR		".dword"
+#define RISCV_SZPTR		"8"
+#define RISCV_LGPTR		"3"
+#endif
+#elif __SIZEOF_POINTER__ == 4
+#ifdef __ASSEMBLY__
+#define RISCV_PTR		.word
+#define RISCV_SZPTR		4
+#define RISCV_LGPTR		2
+#else
+#define RISCV_PTR		".word"
+#define RISCV_SZPTR		"4"
+#define RISCV_LGPTR		"2"
+#endif
+#else
+#error "Unexpected __SIZEOF_POINTER__"
+#endif
+
+#if (__SIZEOF_INT__ == 4)
+#define INT		__ASM_STR(.word)
+#define SZINT		__ASM_STR(4)
+#define LGINT		__ASM_STR(2)
+#else
+#error "Unexpected __SIZEOF_INT__"
+#endif
+
+#if (__SIZEOF_SHORT__ == 2)
+#define SHORT		__ASM_STR(.half)
+#define SZSHORT		__ASM_STR(2)
+#define LGSHORT		__ASM_STR(1)
+#else
+#error "Unexpected __SIZEOF_SHORT__"
+#endif
+
+#endif /* _ASM_RISCV_ASM_H */
arch/riscv/include/asm/bitops.h +0 -4
index 27e47858c6b1..d4f1c9c66e4d 100644
@@ -18,8 +18,6 @@
 #error "Only <linux/bitops.h> can be included directly"
 #endif /* _LINUX_BITOPS_H */
 
-#ifdef __KERNEL__
-
 #include <linux/compiler.h>
 #include <linux/irqflags.h>
 #include <asm/barrier.h>
@@ -223,6 +221,4 @@ static inline void __clear_bit_unlock(
 
 #endif /* CONFIG_ISA_A */
 
-#endif /* __KERNEL__ */
-
 #endif /* _ASM_RISCV_BITOPS_H */
arch/riscv/include/asm/csr.h new file +125 -0

125 lines not on this page · raw message

arch/riscv/include/asm/linkage.h new file +20 -0
new file mode 100644
index 000000000000..b7b304ca89c4
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_LINKAGE_H
+#define _ASM_RISCV_LINKAGE_H
+
+#define __ALIGN		.balign 4
+#define __ALIGN_STR	".balign 4"
+
+#endif /* _ASM_RISCV_LINKAGE_H */
arch/riscv/include/asm/string.h new file +26 -0
new file mode 100644
index 000000000000..9210fcf4ff52
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2013 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_STRING_H
+#define _ASM_RISCV_STRING_H
+
+#include <linux/types.h>
+#include <linux/linkage.h>
+
+#define __HAVE_ARCH_MEMSET
+extern asmlinkage void *memset(void *, int, size_t);
+
+#define __HAVE_ARCH_MEMCPY
+extern asmlinkage void *memcpy(void *, const void *, size_t);
+
+#endif /* _ASM_RISCV_STRING_H */
arch/riscv/include/asm/uaccess.h new file +441 -0

441 lines not on this page · raw message

arch/riscv/include/asm/word-at-a-time.h new file +55 -0

55 lines not on this page · raw message

arch/riscv/kernel/stacktrace.c new file +177 -0

177 lines not on this page · raw message

arch/riscv/lib/memcpy.S new file +100 -0

100 lines not on this page · raw message

arch/riscv/lib/memset.S new file +120 -0

120 lines not on this page · raw message

arch/riscv/lib/uaccess.S new file +127 -0

127 lines not on this page · raw message

arch/riscv/lib/udivdi3.S new file +38 -0
new file mode 100644
index 000000000000..cb01ae5b181a
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2016-2017 Free Software Foundation, Inc.
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+  .globl __udivdi3
+__udivdi3:
+  mv    a2, a1
+  mv    a1, a0
+  li    a0, -1
+  beqz  a2, .L5
+  li    a3, 1
+  bgeu  a2, a1, .L2
+.L1:
+  blez  a2, .L2
+  slli  a2, a2, 1
+  slli  a3, a3, 1
+  bgtu  a1, a2, .L1
+.L2:
+  li    a0, 0
+.L3:
+  bltu  a1, a2, .L4
+  sub   a1, a1, a2
+  or    a0, a0, a3
+.L4:
+  srli  a3, a3, 1
+  srli  a2, a2, 1
+  bnez  a3, .L3
+.L5:
+  ret
Signature · 1 line
2.13.0
author maintainer
#27 lore raw

PATCH 6/9 RISC-V: Device, timer, IRQs, and the SBI

In reply to Palmer Dabbelt

This patch contains code that interfaces with devices that are mandated by the RISC-V supervisor specification and that don't have explicit drivers anywhere else in the tree. This includes the staticly defined interrupts, the CSR-mapped timer, and virtualized SBI devices.

---
 arch/riscv/include/asm/delay.h       |  28 +++++++++
 arch/riscv/include/asm/dma-mapping.h |  38 ++++++++++++
 arch/riscv/include/asm/irq.h         |  31 ++++++++++
 arch/riscv/include/asm/irqflags.h    |  63 ++++++++++++++++++++
 arch/riscv/include/asm/pci.h         |  48 +++++++++++++++
 arch/riscv/include/asm/sbi.h         | 100 +++++++++++++++++++++++++++++++
 arch/riscv/include/asm/timex.h       |  59 +++++++++++++++++++
 arch/riscv/lib/delay.c               | 110 +++++++++++++++++++++++++++++++++++
 arch/riscv/mm/ioremap.c              |  92 +++++++++++++++++++++++++++++
 9 files changed, 569 insertions(+)
 create mode 100644 arch/riscv/include/asm/delay.h
 create mode 100644 arch/riscv/include/asm/dma-mapping.h
 create mode 100644 arch/riscv/include/asm/irq.h
 create mode 100644 arch/riscv/include/asm/irqflags.h
 create mode 100644 arch/riscv/include/asm/pci.h
 create mode 100644 arch/riscv/include/asm/sbi.h
 create mode 100644 arch/riscv/include/asm/timex.h
 create mode 100644 arch/riscv/lib/delay.c
 create mode 100644 arch/riscv/mm/ioremap.c
arch/riscv/include/asm/delay.h new file +28 -0
new file mode 100644
index 000000000000..cbb0c9eb96cb
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2009 Chen Liqin <[email protected]>
+ * Copyright (C) 2016 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_DELAY_H
+#define _ASM_RISCV_DELAY_H
+
+extern unsigned long riscv_timebase;
+
+#define udelay udelay
+extern void udelay(unsigned long usecs);
+
+#define ndelay ndelay
+extern void ndelay(unsigned long nsecs);
+
+extern void __delay(unsigned long cycles);
+
+#endif /* _ASM_RISCV_DELAY_H */
arch/riscv/include/asm/dma-mapping.h new file +38 -0
new file mode 100644
index 000000000000..3eec1000196d
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2003-2004 Hewlett-Packard Co
+ *	David Mosberger-Tang <[email protected]>
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2016 SiFive, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __ASM_RISCV_DMA_MAPPING_H
+#define __ASM_RISCV_DMA_MAPPING_H
+
+/* Use ops->dma_mapping_error (if it exists) or assume success */
+// #undef DMA_ERROR_CODE
+
+static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
+{
+	return &dma_noop_ops;
+}
+
+static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
+{
+	if (!dev->dma_mask)
+		return false;
+
+	return addr + size - 1 <= *dev->dma_mask;
+}
+
+#endif	/* __ASM_RISCV_DMA_MAPPING_H */
arch/riscv/include/asm/irq.h new file +31 -0
new file mode 100644
index 000000000000..e64c61e89f76
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2017 SiFive
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_IRQ_H
+#define _ASM_RISCV_IRQ_H
+
+#define NR_IRQS         0
+
+#define INTERRUPT_CAUSE_SOFTWARE    1
+#define INTERRUPT_CAUSE_TIMER       5
+#define INTERRUPT_CAUSE_EXTERNAL    9
+
+void riscv_timer_interrupt(void);
+
+#include <asm-generic/irq.h>
+
+/* The value of csr sie before init_traps runs (core is up) */
+DECLARE_PER_CPU(atomic_long_t, riscv_early_sie);
+
+#endif /* _ASM_RISCV_IRQ_H */
arch/riscv/include/asm/irqflags.h new file +63 -0

63 lines not on this page · raw message

arch/riscv/include/asm/pci.h new file +48 -0

48 lines not on this page · raw message

arch/riscv/include/asm/sbi.h new file +100 -0

100 lines not on this page · raw message

arch/riscv/include/asm/timex.h new file +59 -0

59 lines not on this page · raw message

arch/riscv/lib/delay.c new file +110 -0

110 lines not on this page · raw message

arch/riscv/mm/ioremap.c new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#29 lore raw

PATCH 7/9 RISC-V: Paging and MMU

In reply to Palmer Dabbelt

This patch contains code to manage the RISC-V MMU, including definitions of the page tables and the page walking code.

---
 arch/riscv/include/asm/mmu_context.h  |  69 ++++++
 arch/riscv/include/asm/page.h         | 134 +++++++++++
 arch/riscv/include/asm/pgalloc.h      | 124 ++++++++++
 arch/riscv/include/asm/pgtable-32.h   |  25 ++
 arch/riscv/include/asm/pgtable-64.h   |  84 +++++++
 arch/riscv/include/asm/pgtable-bits.h |  48 ++++
 arch/riscv/include/asm/pgtable.h      | 430 ++++++++++++++++++++++++++++++++++
 arch/riscv/mm/fault.c                 | 282 ++++++++++++++++++++++
 8 files changed, 1196 insertions(+)
 create mode 100644 arch/riscv/include/asm/mmu_context.h
 create mode 100644 arch/riscv/include/asm/page.h
 create mode 100644 arch/riscv/include/asm/pgalloc.h
 create mode 100644 arch/riscv/include/asm/pgtable-32.h
 create mode 100644 arch/riscv/include/asm/pgtable-64.h
 create mode 100644 arch/riscv/include/asm/pgtable-bits.h
 create mode 100644 arch/riscv/include/asm/pgtable.h
 create mode 100644 arch/riscv/mm/fault.c
arch/riscv/include/asm/mmu_context.h new file +69 -0
new file mode 100644
index 000000000000..de1fc1631fc4
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_MMU_CONTEXT_H
+#define _ASM_RISCV_MMU_CONTEXT_H
+
+#include <asm-generic/mm_hooks.h>
+
+#include <linux/mm.h>
+#include <linux/sched.h>
+#include <asm/tlbflush.h>
+
+static inline void enter_lazy_tlb(struct mm_struct *mm,
+	struct task_struct *task)
+{
+}
+
+/* Initialize context-related info for a new mm_struct */
+static inline int init_new_context(struct task_struct *task,
+	struct mm_struct *mm)
+{
+	return 0;
+}
+
+static inline void destroy_context(struct mm_struct *mm)
+{
+}
+
+static inline pgd_t *current_pgdir(void)
+{
+	return pfn_to_virt(csr_read(sptbr) & SPTBR_PPN);
+}
+
+static inline void set_pgdir(pgd_t *pgd)
+{
+	csr_write(sptbr, virt_to_pfn(pgd) | SPTBR_MODE);
+}
+
+static inline void switch_mm(struct mm_struct *prev,
+	struct mm_struct *next, struct task_struct *task)
+{
+	if (likely(prev != next)) {
+		set_pgdir(next->pgd);
+		local_flush_tlb_all();
+	}
+}
+
+static inline void activate_mm(struct mm_struct *prev,
+			       struct mm_struct *next)
+{
+	switch_mm(prev, next, NULL);
+}
+
+static inline void deactivate_mm(struct task_struct *task,
+	struct mm_struct *mm)
+{
+}
+
+#endif /* _ASM_RISCV_MMU_CONTEXT_H */
arch/riscv/include/asm/page.h new file +134 -0

134 lines not on this page · raw message

arch/riscv/include/asm/pgalloc.h new file +124 -0

124 lines not on this page · raw message

arch/riscv/include/asm/pgtable-32.h new file +25 -0
new file mode 100644
index 000000000000..d61974b74182
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_PGTABLE_32_H
+#define _ASM_RISCV_PGTABLE_32_H
+
+#include <asm-generic/pgtable-nopmd.h>
+#include <linux/const.h>
+
+/* Size of region mapped by a page global directory */
+#define PGDIR_SHIFT     22
+#define PGDIR_SIZE      (_AC(1, UL) << PGDIR_SHIFT)
+#define PGDIR_MASK      (~(PGDIR_SIZE - 1))
+
+#endif /* _ASM_RISCV_PGTABLE_32_H */
arch/riscv/include/asm/pgtable-64.h new file +84 -0

84 lines not on this page · raw message

arch/riscv/include/asm/pgtable-bits.h new file +48 -0

48 lines not on this page · raw message

arch/riscv/include/asm/pgtable.h new file +430 -0

430 lines not on this page · raw message

arch/riscv/mm/fault.c new file +282 -0

282 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#31 lore raw

PATCH 8/9 RISC-V: User-facing API

In reply to Palmer Dabbelt

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.

---
 arch/riscv/include/asm/elf.h              |   3 +-
 arch/riscv/include/asm/mmu.h              |  26 +++
 arch/riscv/include/asm/ptrace.h           | 116 ++++++++++++
 arch/riscv/include/asm/syscall.h          |  90 ++++++++++
 arch/riscv/include/asm/syscalls.h         |  25 +++
 arch/riscv/include/asm/unistd.h           |  16 ++
 arch/riscv/include/asm/vdso.h             |  32 ++++
 arch/riscv/include/uapi/asm/auxvec.h      |  24 +++
 arch/riscv/include/uapi/asm/bitsperlong.h |  25 +++
 arch/riscv/include/uapi/asm/byteorder.h   |  23 +++
 arch/riscv/include/uapi/asm/elf.h         |  83 +++++++++
 arch/riscv/include/uapi/asm/hwcap.h       |  36 ++++
 arch/riscv/include/uapi/asm/ptrace.h      |  90 ++++++++++
 arch/riscv/include/uapi/asm/sigcontext.h  |  30 ++++
 arch/riscv/include/uapi/asm/siginfo.h     |  24 +++
 arch/riscv/include/uapi/asm/ucontext.h    |  35 ++++
 arch/riscv/include/uapi/asm/unistd.h      |  23 +++
 arch/riscv/kernel/cpufeature.c            |  61 +++++++
 arch/riscv/kernel/module.c                | 217 ++++++++++++++++++++++
 arch/riscv/kernel/ptrace.c                | 138 ++++++++++++++
 arch/riscv/kernel/riscv_ksyms.c           |  15 ++
 arch/riscv/kernel/signal.c                | 289 ++++++++++++++++++++++++++++++
 arch/riscv/kernel/sys_riscv.c             |  90 ++++++++++
 arch/riscv/kernel/syscall_table.c         |  25 +++
 arch/riscv/kernel/vdso/.gitignore         |   1 +
 arch/riscv/kernel/vdso/cmpxchg32.S        |  40 +++++
 arch/riscv/kernel/vdso/cmpxchg64.S        |  40 +++++
 arch/riscv/kernel/vdso/sigreturn.S        |  24 +++
 arch/riscv/kernel/vdso/vdso.S             |  27 +++
 29 files changed, 1667 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/include/asm/mmu.h
 create mode 100644 arch/riscv/include/asm/ptrace.h
 create mode 100644 arch/riscv/include/asm/syscall.h
 create mode 100644 arch/riscv/include/asm/syscalls.h
 create mode 100644 arch/riscv/include/asm/unistd.h
 create mode 100644 arch/riscv/include/asm/vdso.h
 create mode 100644 arch/riscv/include/uapi/asm/auxvec.h
 create mode 100644 arch/riscv/include/uapi/asm/bitsperlong.h
 create mode 100644 arch/riscv/include/uapi/asm/byteorder.h
 create mode 100644 arch/riscv/include/uapi/asm/elf.h
 create mode 100644 arch/riscv/include/uapi/asm/hwcap.h
 create mode 100644 arch/riscv/include/uapi/asm/ptrace.h
 create mode 100644 arch/riscv/include/uapi/asm/sigcontext.h
 create mode 100644 arch/riscv/include/uapi/asm/siginfo.h
 create mode 100644 arch/riscv/include/uapi/asm/ucontext.h
 create mode 100644 arch/riscv/include/uapi/asm/unistd.h
 create mode 100644 arch/riscv/kernel/cpufeature.c
 create mode 100644 arch/riscv/kernel/module.c
 create mode 100644 arch/riscv/kernel/ptrace.c
 create mode 100644 arch/riscv/kernel/riscv_ksyms.c
 create mode 100644 arch/riscv/kernel/signal.c
 create mode 100644 arch/riscv/kernel/sys_riscv.c
 create mode 100644 arch/riscv/kernel/syscall_table.c
 create mode 100644 arch/riscv/kernel/vdso/.gitignore
 create mode 100644 arch/riscv/kernel/vdso/cmpxchg32.S
 create mode 100644 arch/riscv/kernel/vdso/cmpxchg64.S
 create mode 100644 arch/riscv/kernel/vdso/sigreturn.S
 create mode 100644 arch/riscv/kernel/vdso/vdso.S
arch/riscv/include/asm/elf.h +2 -1
index 5ded3f6f83ea..a1ef503d616e 100644
@@ -59,7 +59,8 @@
  * instruction set this CPU supports.  This could be done in user space,
  * but it's not easy, and we've already done it here.
  */
-#define ELF_HWCAP	(0)
+#define ELF_HWCAP	(elf_hwcap)
+extern unsigned long elf_hwcap;
 
 /*
  * This yields a string that ld.so will use to load implementation
arch/riscv/include/asm/mmu.h new file +26 -0
new file mode 100644
index 000000000000..66805cba9a27
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+
+#ifndef _ASM_RISCV_MMU_H
+#define _ASM_RISCV_MMU_H
+
+#ifndef __ASSEMBLY__
+
+typedef struct {
+	void *vdso;
+} mm_context_t;
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* _ASM_RISCV_MMU_H */
arch/riscv/include/asm/ptrace.h new file +116 -0

116 lines not on this page · raw message

arch/riscv/include/asm/syscall.h new file +90 -0

90 lines not on this page · raw message

arch/riscv/include/asm/syscalls.h new file +25 -0
new file mode 100644
index 000000000000..6490274fbb76
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2014 Darius Rad <[email protected]>
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_SYSCALLS_H
+#define _ASM_RISCV_SYSCALLS_H
+
+#include <linux/linkage.h>
+
+#include <asm-generic/syscalls.h>
+
+/* kernel/sys_riscv.c */
+asmlinkage long sys_sysriscv_cmpxchg32(u32 __user * ptr, u32 new, u32 old);
+asmlinkage long sys_sysriscv_cmpxchg64(u64 __user * ptr, u64 new, u64 old);
+
+#endif /* _ASM_RISCV_SYSCALLS_H */
arch/riscv/include/asm/unistd.h new file +16 -0
new file mode 100644
index 000000000000..9f250ed007cd
@@ -0,0 +1,16 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#define __ARCH_HAVE_MMU
+#define __ARCH_WANT_SYS_CLONE
+#include <uapi/asm/unistd.h>
arch/riscv/include/asm/vdso.h new file +32 -0
new file mode 100644
index 000000000000..95768a3810a7
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2012 ARM Limited
+ * Copyright (C) 2014 Regents of the University of California
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _ASM_RISCV_VDSO_H
+#define _ASM_RISCV_VDSO_H
+
+#include <linux/types.h>
+
+struct vdso_data {
+};
+
+#define VDSO_SYMBOL(base, name)					\
+({								\
+	extern const char __vdso_##name[];			\
+	(void __user *)((unsigned long)(base) + __vdso_##name);	\
+})
+
+#endif /* _ASM_RISCV_VDSO_H */
arch/riscv/include/uapi/asm/auxvec.h new file +24 -0
new file mode 100644
index 000000000000..1376515547cd
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _UAPI_ASM_RISCV_AUXVEC_H
+#define _UAPI_ASM_RISCV_AUXVEC_H
+
+/* vDSO location */
+#define AT_SYSINFO_EHDR 33
+
+#endif /* _UAPI_ASM_RISCV_AUXVEC_H */
arch/riscv/include/uapi/asm/bitsperlong.h new file +25 -0
new file mode 100644
index 000000000000..0b3cb52fd29d
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _UAPI_ASM_RISCV_BITSPERLONG_H
+#define _UAPI_ASM_RISCV_BITSPERLONG_H
+
+#define __BITS_PER_LONG (__SIZEOF_POINTER__ * 8)
+
+#include <asm-generic/bitsperlong.h>
+
+#endif /* _UAPI_ASM_RISCV_BITSPERLONG_H */
arch/riscv/include/uapi/asm/byteorder.h new file +23 -0
new file mode 100644
index 000000000000..4ca38af2cd32
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2015 Regents of the University of California
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _UAPI_ASM_RISCV_BYTEORDER_H
+#define _UAPI_ASM_RISCV_BYTEORDER_H
+
+#include <linux/byteorder/little_endian.h>
+
+#endif /* _UAPI_ASM_RISCV_BYTEORDER_H */
arch/riscv/include/uapi/asm/elf.h new file +83 -0

83 lines not on this page · raw message

arch/riscv/include/uapi/asm/hwcap.h new file +36 -0
new file mode 100644
index 000000000000..f333221c9ab2
@@ -0,0 +1,36 @@
+/*
+ * Copied from arch/arm64/include/asm/hwcap.h
+ *
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2017 SiFive
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __UAPI_ASM_HWCAP_H
+#define __UAPI_ASM_HWCAP_H
+
+/*
+ * Linux saves the floating-point registers according to the ISA Linux is
+ * executing on, as opposed to the ISA the user program is compiled for.  This
+ * is necessary for a handful of esoteric use cases: for example, userpsace
+ * threading libraries must be able to examine the actual machine state in
+ * order to fully reconstruct the state of a thread.
+ */
+#define COMPAT_HWCAP_ISA_I	(1 << ('I' - 'A'))
+#define COMPAT_HWCAP_ISA_M	(1 << ('M' - 'A'))
+#define COMPAT_HWCAP_ISA_A	(1 << ('A' - 'A'))
+#define COMPAT_HWCAP_ISA_F	(1 << ('F' - 'A'))
+#define COMPAT_HWCAP_ISA_D	(1 << ('D' - 'A'))
+#define COMPAT_HWCAP_ISA_C	(1 << ('C' - 'A'))
+
+#endif
arch/riscv/include/uapi/asm/ptrace.h new file +90 -0

90 lines not on this page · raw message

arch/riscv/include/uapi/asm/sigcontext.h new file +30 -0
new file mode 100644
index 000000000000..ed7372b277fa
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _UAPI_ASM_RISCV_SIGCONTEXT_H
+#define _UAPI_ASM_RISCV_SIGCONTEXT_H
+
+#include <asm/ptrace.h>
+
+/*
+ * Signal context structure
+ *
+ * This contains the context saved before a signal handler is invoked;
+ * it is restored by sys_sigreturn / sys_rt_sigreturn.
+ */
+struct sigcontext {
+	struct user_regs_struct sc_regs;
+	union __riscv_fp_state sc_fpregs;
+};
+
+#endif /* _UAPI_ASM_RISCV_SIGCONTEXT_H */
arch/riscv/include/uapi/asm/siginfo.h new file +24 -0
new file mode 100644
index 000000000000..f96849aac662
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2016 SiFive, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __ASM_SIGINFO_H
+#define __ASM_SIGINFO_H
+
+#define __ARCH_SI_PREAMBLE_SIZE	(__SIZEOF_POINTER__ == 4 ? 12 : 16)
+
+#include <asm-generic/siginfo.h>
+
+#endif
arch/riscv/include/uapi/asm/ucontext.h new file +35 -0
new file mode 100644
index 000000000000..52eff9febcfd
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2012 ARM Ltd.
+ * Copyright (C) 2017 SiFive, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * This file was copied from arch/arm64/include/uapi/asm/ucontext.h
+ */
+#ifndef _UAPI__ASM_UCONTEXT_H
+#define _UAPI__ASM_UCONTEXT_H
+
+#include <linux/types.h>
+
+struct ucontext {
+	unsigned long	  uc_flags;
+	struct ucontext	 *uc_link;
+	stack_t		  uc_stack;
+	sigset_t	  uc_sigmask;
+	/* glibc uses a 1024-bit sigset_t */
+	__u8		  __unused[1024 / 8 - sizeof(sigset_t)];
+	/* last for future expansion */
+	struct sigcontext uc_mcontext;
+};
+
+#endif /* _UAPI__ASM_UCONTEXT_H */
arch/riscv/include/uapi/asm/unistd.h new file +23 -0
new file mode 100644
index 000000000000..37a5429cc896
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <asm-generic/unistd.h>
+
+/*
+ * These system calls add support for AMOs on RISC-V systems without support
+ * for the A extension.
+ */
+#define __NR_sysriscv_cmpxchg32		(__NR_arch_specific_syscall + 0)
+__SYSCALL(__NR_sysriscv_cmpxchg32, sys_sysriscv_cmpxchg32)
+#define __NR_sysriscv_cmpxchg64		(__NR_arch_specific_syscall + 1)
+__SYSCALL(__NR_sysriscv_cmpxchg64, sys_sysriscv_cmpxchg64)
arch/riscv/kernel/cpufeature.c new file +61 -0

61 lines not on this page · raw message

arch/riscv/kernel/module.c new file +217 -0

217 lines not on this page · raw message

arch/riscv/kernel/ptrace.c new file +138 -0

138 lines not on this page · raw message

arch/riscv/kernel/riscv_ksyms.c new file +15 -0
new file mode 100644
index 000000000000..23cc81ec9e94
@@ -0,0 +1,15 @@
+/*
+ * Copyright (C) 2017 Zihao Yu
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/export.h>
+#include <linux/uaccess.h>
+
+/*
+ * Assembly functions that may be used (directly or indirectly) by modules
+ */
+EXPORT_SYMBOL(__copy_user);
arch/riscv/kernel/signal.c new file +289 -0

289 lines not on this page · raw message

arch/riscv/kernel/sys_riscv.c new file +90 -0

90 lines not on this page · raw message

arch/riscv/kernel/syscall_table.c new file +25 -0
new file mode 100644
index 000000000000..8fa239b67cbc
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2009 Arnd Bergmann <[email protected]>
+ * Copyright (C) 2012 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/syscalls.h>
+
+#include <asm/syscalls.h>
+
+#undef __SYSCALL
+#define __SYSCALL(nr, call)	[nr] = (call),
+
+void *sys_call_table[__NR_syscalls] = {
+	[0 ... __NR_syscalls - 1] = sys_ni_syscall,
+#include <asm/unistd.h>
+};
arch/riscv/kernel/vdso/.gitignore new file +1 -0
new file mode 100644
index 000000000000..f8b69d84238e
@@ -0,0 +1 @@
+vdso.lds
arch/riscv/kernel/vdso/cmpxchg32.S new file +40 -0
new file mode 100644
index 000000000000..387deb4e852c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2017 SiFive
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/linkage.h>
+#include <asm/unistd.h>
+
+	.text
+ENTRY(__vdso_cmpxchg32)
+	.cfi_startproc
+#ifdef CONFIG_ISA_A
+/*
+ * a0: ptr
+ * a1: compare value
+ * a2: exchange value
+ */
+0:
+	lr.w.aqrl t0, 0(a0)
+	bne       t0, a1, 1f
+	sc.w.aqrl t1, a2, 0(a0)
+	bnez      t1, 0b
+1:
+	mv        a0, t0
+	ret
+#else
+	li a7, __NR_sysriscv_cmpxchg32
+	scall
+	ret
+#endif
+	.cfi_endproc
+ENDPROC(__vdso_cmpxchg32)
arch/riscv/kernel/vdso/cmpxchg64.S new file +40 -0
new file mode 100644
index 000000000000..6286842f78c0
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2017 SiFive
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/linkage.h>
+#include <asm/unistd.h>
+
+	.text
+ENTRY(__vdso_cmpxchg64)
+	.cfi_startproc
+#ifdef CONFIG_ISA_A
+/*
+ * a0: ptr
+ * a1: compare value
+ * a2: exchange value
+ */
+0:
+	lr.d.aqrl t0, 0(a0)
+	bne       t0, a1, 1f
+	sc.d.aqrl t1, a2, 0(a0)
+	bnez      t1, 0b
+1:
+	mv        a0, t0
+	ret
+#else
+	li a7, __NR_sysriscv_cmpxchg64
+	scall
+	ret
+#endif
+	.cfi_endproc
+ENDPROC(__vdso_cmpxchg64)
arch/riscv/kernel/vdso/sigreturn.S new file +24 -0
new file mode 100644
index 000000000000..f5aa3d72acfb
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2014 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/linkage.h>
+#include <asm/unistd.h>
+
+	.text
+ENTRY(__vdso_rt_sigreturn)
+	.cfi_startproc
+	.cfi_signal_frame
+	li a7, __NR_rt_sigreturn
+	scall
+	.cfi_endproc
+ENDPROC(__vdso_rt_sigreturn)
arch/riscv/kernel/vdso/vdso.S new file +27 -0
new file mode 100644
index 000000000000..7055de5f9174
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2014 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#include <linux/init.h>
+#include <linux/linkage.h>
+#include <asm/page.h>
+
+	__PAGE_ALIGNED_DATA
+
+	.globl vdso_start, vdso_end
+	.balign PAGE_SIZE
+vdso_start:
+	.incbin "arch/riscv/kernel/vdso/vdso.so"
+	.balign PAGE_SIZE
+vdso_end:
+
+	.previous
Signature · 1 line
2.13.0
#3 lore raw

Re: [patches] Re: [PATCH 1/9] RISC-V: Init and Halt Code

In reply to a message not in the archive
Quoted 33 lines · Thomas Gleixner
Quoted 31 lines · Palmer Dabbelt
arch/riscv/kernel/time.c new file +31 -0
+unsigned long riscv_timebase;
+
+static int next_event(unsigned long delta, struct clock_event_device *ce)
+{
+	BUG_ON(ce != timer_riscv_device(smp_processor_id()));
+	sbi_set_timer(get_cycles64() + delta);
+	return 0;
+}
+
+static unsigned long long rdtime(struct clocksource *cs)
+{
+	BUG_ON(cs != timer_riscv_source(smp_processor_id()));
+	return get_cycles64();
+}
+
+void riscv_timer_interrupt(void)
+{
+	int cpu = smp_processor_id();
+	struct clock_event_device *evdev = timer_riscv_device(cpu);
+
+	evdev->event_handler(evdev);
+}
+
+void __init time_init(void)
+{
+	struct device_node *cpu;
+	u32 prop;
+	int cpu_id = smp_processor_id();
+
+	cpu = of_find_node_by_path("/cpus");
+	if (!cpu || of_property_read_u32(cpu, "timebase-frequency", &prop))
Quoted 8 lines

arch/riscv/kernel/time.c line 58

Quoted 6 lines
arch/riscv/kernel/time.c new file +6 -0
+		panic(KERN_WARNING "RISC-V system with no 'timebase-frequency' in DTS\n");
+	riscv_timebase = prop;
+
+	lpj_fine = riscv_timebase / HZ;
+
+	timer_riscv_init(cpu_id, riscv_timebase, &rdtime, &next_event);

This function is nowhere.... Neither can I find the header file.

It is part of the RISC-V clock driver patchset, please cf.
https://github.com/riscv/riscv-linux/commit/0705a31372d4714e54c5104e02021441785c1fd5

https://github.com/riscv/riscv-linux/commits/riscv-for-submission-v4
has the various RISC-V-related driver patchsets merged in (in addition to this arch patchset).

Regards,
Karsten

Signature · 3 lines
Gem. Par. 28 Abs. 4 Bundesdatenschutzgesetz widerspreche ich der Nutzung
sowie der Weitergabe meiner personenbezogenen Daten für Zwecke der
Werbung sowie der Markt- oder Meinungsforschung.
#5 lore raw

Re: [patches] [PATCH 1/9] RISC-V: Init and Halt Code

In reply to Palmer Dabbelt

Hi, below are some small comments.

Quoted 3 lines · Palmer Dabbelt

This contains the various __init C functions, the initial assembly kernel entry point, and the code to reset the system. When a file was init-related, it contains

It contains what?

Quoted 1 line

[...]

arch/riscv/include/asm/bug.h line 24

Quoted 2 lines
arch/riscv/include/asm/bug.h new file +2 -0
+#ifdef CONFIG_GENERIC_BUG
+#define __BUG_INSN	_AC(0x00100073, UL) /* sbreak */

This should be ebreak, not sbreak, in Priv Spec 1.10, AFAICT. I guess binutils still understands sbreak, but it's nicer to stick to the spec, IMHO.

arch/riscv/include/asm/bug.h line 51

Quoted 5 lines
arch/riscv/include/asm/bug.h new file +5 -0
+#define BUG()							\
+do {								\
+	__asm__ __volatile__ (					\
+		"1:\n\t"					\
+			"sbreak\n"				\

ebreak

Quoted 6 lines
arch/riscv/include/asm/bug.h new file +6 -0
+			".pushsection __bug_table,\"a\"\n\t"	\
+		"2:\n\t"					\
+			__BUG_ENTRY "\n\t"			\
+			".org 2b + %2\n\t"			\
+			".popsection"				\
+		:						\

arch/riscv/include/asm/bug.h line 67

Quoted 10 lines
arch/riscv/include/asm/bug.h new file +10 -0
+		: "i" (__FILE__), "i" (__LINE__),		\
+		  "i" (sizeof(struct bug_entry)));		\
+	unreachable();						\
+} while (0)
+#endif /* !__ASSEMBLY__ */
+#else /* CONFIG_GENERIC_BUG */
+#ifndef __ASSEMBLY__
+#define BUG()							\
+do {								\
+	__asm__ __volatile__ ("sbreak\n");			\

ebreak

Quoted 4 lines
arch/riscv/include/asm/bug.h new file +4 -0
+	unreachable();						\
+} while (0)
+#endif /* !__ASSEMBLY__ */
+#endif /* CONFIG_GENERIC_BUG */

[...]

arch/riscv/kernel/traps.c line 115

Quoted 12 lines
arch/riscv/kernel/traps.c new file +12 -0
+#define DO_ERROR_INFO(name, signo, code, str)				\
+asmlinkage void name(struct pt_regs *regs)				\
+{									\
+	do_trap_error(regs, signo, code, regs->sepc, "Oops - " str);	\
+}
+
+DO_ERROR_INFO(do_trap_unknown,
+	SIGILL, ILL_ILLTRP, "unknown exception");
+DO_ERROR_INFO(do_trap_insn_misaligned,
+	SIGBUS, BUS_ADRALN, "instruction address misaligned");
+DO_ERROR_INFO(do_trap_insn_fault,
+	SIGBUS, BUS_ADRALN, "instruction access fault");

For a general instruction access fault, BUS_ADRALN seems wrong. A variant of SIGSEGV seems more appropriate, IMHO.

Jonathan Neuschäfer

#4 lore raw

Re: [patches] Re: [PATCH 1/9] RISC-V: Init and Halt Code

In reply to Karsten Merker
Quoted 11 lines · Karsten Merker
Quoted 3 lines · Thomas Gleixner

arch/riscv/kernel/time.c line 58

Quoted 1 line
arch/riscv/kernel/time.c new file +1 -0
+	timer_riscv_init(cpu_id, riscv_timebase, &rdtime, &next_event);

This function is nowhere.... Neither can I find the header file.

It is part of the RISC-V clock driver patchset, please cf.
https://github.com/riscv/riscv-linux/commit/0705a31372d4714e54c5104e02021441785c1fd5

https://github.com/riscv/riscv-linux/commits/riscv-for-submission-v4
has the various RISC-V-related driver patchsets merged in (in addition to this arch patchset).

Well, then please move that timer stuff to the clock driver patchset, so the combo can be reviewed. Having half functional patches with missing references here and there makes review a PITA.

Thanks,

tglx

maintainer
#11 lore raw

Re: [PATCH 2/9] RISC-V: Atomic and Locking Code

In reply to Palmer Dabbelt
Quoted 5 lines · Palmer Dabbelt

+/*
+ * FIXME: I could only find documentation that atomic_{add,sub,inc,dec} are
+ * barrier-free. I'm assuming that and/or/xor have the same constraints as the
+ * others.
+ */

Yes.. we have new documentation in the work to which I would post a link but for some reason copy/paste stopped working again (Konsole does that at times and is #$%#$%#4# annoying).

Ha, found it using google...

https://marc.info/?l=linux-kernel&m=14972790112580

arch/riscv/include/asm/atomic.h line 303

Quoted 7 lines
arch/riscv/include/asm/atomic.h new file +6 -0
+
 
+/*
+ * atomic_{cmp,}xchg is required to have exactly the same ordering semantics as
+ * {cmp,}xchg and the operations that return, so they need a barrier.  We just
+ * use the other implementations directly.
+ */

cmpxchg triggers an extra rule; all conditional operations only need to imply barriers on success. So a cmpxchg that fails, need not imply any ordering what so ever.

arch/riscv/include/asm/barrier.h line 46

Quoted 6 lines
arch/riscv/include/asm/barrier.h new file +6 -0
+/*
+ * Our atomic operations set the AQ and RL bits and therefor we don't need to
+ * fence around atomics.
+ */
+#define __smb_mb__before_atomic()	barrier()
+#define __smb_mb__after_atomic()	barrier()

Ah, not quite... you need full barriers here. Because your regular atomic ops imply no ordering what so ever.

arch/riscv/include/asm/barrier.h line 54

Quoted 7 lines
arch/riscv/include/asm/barrier.h new file +7 -0
+/*
+ * These barries are meant to prevent memory operations inside a spinlock from
+ * moving outside of that spinlock.  Since we set the AQ and RL bits when
+ * entering or leaving spinlocks, no additional fence needs to be performed.
+ */
+#define smb_mb__before_spinlock()	barrier()
+#define smb_mb__after_spinlock()	barrier()

Also probably not true. I _think_ you want a full barrier here, but given the total lack of documentation on your end and the fact I've not yet read the spinlock (which I suppose is below) I cannot yet state more.

arch/riscv/include/asm/barrier.h line 57

Quoted 3 lines
arch/riscv/include/asm/barrier.h new file +3 -0
+
+/* FIXME: I don't think RISC-V is allowed to perform a speculative load. */
+#define smp_acquire__after_ctrl_dep()	barrier()

That would be a very weird thing to disallow... speculative loads are teh awesome ;-) Note you can get the very same effect from caches when your stores are not globally atomic.

Quoted 51 lines
arch/riscv/include/asm/barrier.h new file +51 -0
+/*
+ * The RISC-V ISA doesn't support byte or half-word AMOs, so we fall back to a
+ * regular store and a fence here.  Otherwise we emit an AMO with an AQ or RL
+ * bit set and allow the microarchitecture to avoid the other half of the AMO.
+ */
+#define __smp_store_release(p, v)					\
+do {									\
+	union { typeof(*p) __val; char __c[1]; } __u =			\
+		{ .__val = (__force typeof(*p)) (v) };			\
+	compiletime_assert_atomic_type(*p);				\
+	switch (sizeof(*p)) {						\
+	case 1:								\
+	case 2:								\
+		smb_mb();						\
+		WRITE_ONCE(*p, __u.__val);				\
+		break;							\
+	case 4:								\
+		__asm__ __volatile__ (					\
+			"amoswap.w.rl zero, %1, %0"			\
+			: "+A" (*p), "r" (__u.__val)			\
+			: 						\
+			: "memory");					\
+		break;							\
+	case 8:								\
+		__asm__ __volatile__ (					\
+			"amoswap.d.rl zero, %1, %0"			\
+			: "+A" (*p), "r" (__u.__val)			\
+			: 						\
+			: "memory");					\
+		break;							\
+	}								\
+} while (0)
+
+#define __smp_load_acquire(p)						\
+do {									\
+	union { typeof(*p) __val; char __c[1]; } __u =			\
+		{ .__val = (__force typeof(*p)) (v) };			\
+	compiletime_assert_atomic_type(*p);				\
+	switch (sizeof(*p)) {						\
+	case 1:								\
+	case 2:								\
+		__u.__val = READ_ONCE(*p);				\
+		smb_mb();						\
+		break;							\
+	case 4:								\
+		__asm__ __volatile__ (					\
+			"amoor.w.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\

arch/riscv/include/asm/barrier.h line 119

Quoted 10 lines
arch/riscv/include/asm/barrier.h new file +10 -0
+	case 8:								\
+		__asm__ __volatile__ (					\
+			"amoor.d.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\
+	}								\
+	__u.__val;							\
+} while (0)

'creative' use of amoswap and amoor :-)

You should really look at a normal load with ordering instruction though, that amoor.aq is a rmw and will promote the cacheline to exclusive (and dirty it).

Quoted 32 lines
arch/riscv/include/asm/spinlock.h new file +32 -0
+/*
+ * Simple spin lock operations.  These provide no fairness guarantees.
+ */
+
+/* FIXME: Replace this with a ticket lock, like MIPS. */
+
+#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
+#define arch_spin_is_locked(x)	((x)->lock != 0)
+#define arch_spin_unlock_wait(x) \
+		do { cpu_relax(); } while ((x)->lock)
+
+static inline void arch_spin_unlock(arch_spinlock_t *lock)
+{
+	__asm__ __volatile__ (
+		"amoswap.w.rl x0, x0, %0"
+		: "=A" (lock->lock)
+		:: "memory");
+}
+
+static inline int arch_spin_trylock(arch_spinlock_t *lock)
+{
+	int tmp = 1, busy;
+
+	__asm__ __volatile__ (
+		"amoswap.w.aq %0, %2, %1"
+		: "=r" (busy), "+A" (lock->lock)
+		: "r" (tmp)
+		: "memory");
+
+	return !busy;
+}
+

arch/riscv/include/asm/spinlock.h line 62

Quoted 10 lines
arch/riscv/include/asm/spinlock.h new file +10 -0
+static inline void arch_spin_lock(arch_spinlock_t *lock)
+{
+	while (1) {
+		if (arch_spin_is_locked(lock))
+			continue;
+
+		if (arch_spin_trylock(lock))
+			break;
+	}
+}

OK, so back to smp_mb__{before,after}_spinlock(), that wants to order things like:

	wakeup:					block:

	COND = 1;				p->state = UNINTERRUPTIBLE;
						smp_mb();
	smp_mb__before_spinlock();
	spin_lock(&lock);			if (!COND)
						  schedule()
	if (p->state & state)
		goto out;

And here it is important that the COND store not happen _after_ the p->state load.

Now, your spin_lock() only implies the AQ thing, which should only constraint later load/stores but does nothing for the prior load/stores. So our COND store can drop into the lock and even happen after the p->state load.

So you very much want your smp_mb__{before,after}_spinlock thingies to be full barriers.

#32 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

In reply to Palmer Dabbelt
Quoted 14 lines · Palmer Dabbelt
arch/riscv/kernel/ptrace.c new file +7 -0
new file mode 100644
index 000000000000..2720d5e97354
@@ -0,0 +1,138 @@
 
+/* Put registers back to task. */
+static void putregs(struct task_struct *child, struct pt_regs *uregs)
+{
+	struct pt_regs *regs = task_pt_regs(child);
+	*regs = *uregs;
+}
+

arch/riscv/kernel/ptrace.c line 49

Quoted 10 lines
arch/riscv/kernel/ptrace.c new file +10 -0
+static int riscv_gpr_get(struct task_struct *target,
+			 const struct user_regset *regset,
+			 unsigned int pos, unsigned int count,
+			 void *kbuf, void __user *ubuf)
+{
+	struct pt_regs *regs;
+
+	regs = task_pt_regs(target);
+	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0,
+				   sizeof(*regs));

sizeof(struct pt_regs) > sizeof(struct user_regs_struct), which allows supervisor registers to be copied too. I think you should be using sizeof(struct user_regs_struct) instead.

arch/riscv/kernel/ptrace.c line 61

Quoted 12 lines
arch/riscv/kernel/ptrace.c new file +12 -0
+}
+
+static int riscv_gpr_set(struct task_struct *target,
+			 const struct user_regset *regset,
+			 unsigned int pos, unsigned int count,
+			 const void *kbuf, const void __user *ubuf)
+{
+	int ret;
+	struct pt_regs regs;
+
+	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0,
+				 sizeof(regs));

same

arch/riscv/kernel/ptrace.c line 65

Quoted 4 lines
arch/riscv/kernel/ptrace.c new file +4 -0
+	if (ret)
+		return ret;
+
+	putregs(target, &regs);

you're still copying via the stack without initialising the non-written fields. If userland does a short PTRACE_SETREGSET the remaining fields will be copied from the uninitialised kernel stack and accessible to userland via PTRACE_GETREGSET.

Even if the user does a full sizeof(struct user_regs_struct) (not pt_regs) PTRACE_SETREGSET the supervisor registers will be overwritten with uninitialised stack content.

Quoted 7 lines
arch/riscv/kernel/sys_riscv.c new file +1 -0
new file mode 100644
index 000000000000..4419604ff46c
 
+SYSCALL_DEFINE3(sysriscv_cmpxchg32, u32 __user *, ptr, u32, new, u32, old)

arch/riscv/kernel/sys_riscv.c line 55

Quoted 10 lines
arch/riscv/kernel/sys_riscv.c new file +10 -0
+{
+	u32 prev;
+	unsigned int err;
+
+	if (!access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)))
+		return -EFAULT;
+
+#ifdef CONFIG_ISA_A
+	err = 0;
+	prev = cmpxchg32(ptr, old, new);

I think this needs a special version of cmpxchg (or for cmpxchg to be modified) with fixup protection to return -EFAULT in case the page isn't mapped or is paged out/read only.

Quoted 13 lines
arch/riscv/kernel/sys_riscv.c new file +13 -0
+#else
+	preempt_disable();
+	err = __get_user(prev, ptr);
+	if (likely(!err && prev == old))
+		err = __put_user(new, ptr);
+	preempt_enable();
+#endif
+
+	return unlikely(err) ? err : prev;
+}
+
+SYSCALL_DEFINE3(sysriscv_cmpxchg64, u64 __user *, ptr, u64, new, u64, old)
+{

arch/riscv/kernel/sys_riscv.c line 78

Quoted 10 lines
arch/riscv/kernel/sys_riscv.c new file +10 -0
+#ifdef CONFIG_64BIT
+	u64 prev;
+	unsigned int err;
+
+	if (!access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)))
+		return -EFAULT;
+
+#ifdef CONFIG_ISA_A
+	err = 0;
+	prev = cmpxchg64(ptr, old, new);

Likewise

arch/riscv/kernel/sys_riscv.c line 88

Quoted 10 lines
arch/riscv/kernel/sys_riscv.c new file +10 -0
+#else
+	preempt_disable();
+	err = __get_user(prev, ptr);
+	if (likely(!err && prev == old))
+		err = __put_user(new, ptr);
+	preempt_enable();
+#endif
+	return unlikely(err) ? err : prev;
+#else
+	return -ENOTSUPP;

I think -ENOSYS is more standard for missing/unimplemented system calls.

A better way IMO would be to #ifdef the definitions in unistd.h, then the __NR_* definitions could also be more accurately extracted from the kernel headers, and you could just ifdef CONFIG_64BIT the whole syscall implementation, i.e.:

arch/riscv/include/uapi/asm/unistd.h line 21

Quoted 13 lines
arch/riscv/include/uapi/asm/unistd.h new file +6 -0
new file mode 100644
index 000000000000..37a5429cc896
@@ -0,0 +1,23 @@
 
+/*
+ * These system calls add support for AMOs on RISC-V systems without support
+ * for the A extension.
+ */
+#define __NR_sysriscv_cmpxchg32		(__NR_arch_specific_syscall + 0)
+__SYSCALL(__NR_sysriscv_cmpxchg32, sys_sysriscv_cmpxchg32)

+ifdef WHATEVER_BUILTIN_GCC_DEFINES_FOR_64BIT_RISCV_ABI

In case its helpful the following should list builtin preprocessor defines for given CFLAGS:
${CROSS_COMPILE}gcc ${CFLAGS} -dM -E -</dev/null

arch/riscv/include/uapi/asm/unistd.h line 23

Quoted 2 lines
arch/riscv/include/uapi/asm/unistd.h new file +2 -0
+#define __NR_sysriscv_cmpxchg64		(__NR_arch_specific_syscall + 1)
+__SYSCALL(__NR_sysriscv_cmpxchg64, sys_sysriscv_cmpxchg64)

+endif

Cheers
James

#33 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

In reply to Palmer Dabbelt

I'm a bit concerned about these cmpxchg syscalls, and I'd like to understand if my concerns are justified.

For a new instruction set that starts out in the 201x years we really should have cmpxchg as a mandatory instruction - if not in the CPU it should be in the Linux ABI so that we don't have to deal with a mess where programs will either need an indirection for CPUs that have cmpxchg capabilities vs those that don't, and we don't need to worry if given binaries work on all CPUs.

What keeps from from declaring that the RISV-A extension is mandatory for Linux?

#19 lore raw

Re: [PATCH 2/9] RISC-V: Atomic and Locking Code

In reply to Palmer Dabbelt

On Tue, Jul 04, 2017 at 12:50:55PM -0700, Palmer Dabbelt wrote:
[...]

Quoted 29 lines
arch/riscv/include/asm/cmpxchg.h new file +23 -0
new file mode 100644
index 000000000000..81025c056412
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2014 Regents of the University of California
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation, version 2.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ */
+
+#ifndef _ASM_RISCV_CMPXCHG_H
+#define _ASM_RISCV_CMPXCHG_H
+
+#include <linux/bug.h>
+
+#ifdef CONFIG_ISA_A
+
+#include <asm/barrier.h>
+
+#define __xchg(new, ptr, size, asm_or)				\

arch/riscv/include/asm/cmpxchg.h line 33

Quoted 10 lines
arch/riscv/include/asm/cmpxchg.h new file +10 -0
+({								\
+	__typeof__(ptr) __ptr = (ptr);				\
+	__typeof__(new) __new = (new);				\
+	__typeof__(*(ptr)) __ret;				\
+	switch (size) {						\
+	case 4:							\
+		__asm__ __volatile__ (				\
+			"amoswap.w" #asm_or " %0, %2, %1"	\
+			: "=r" (__ret), "+A" (*__ptr)		\
+			: "r" (__new));				\

It seems that you miss the "memmory" clobber here, so as for cmpxchg(), did you do this on purpose? AFAIK, without this clobber, compilers are within their right to reorder operations preceding and following this operation, which makes it unordered.

Quoted 27 lines
arch/riscv/include/asm/cmpxchg.h new file +27 -0
+		break;						\
+	case 8:							\
+		__asm__ __volatile__ (				\
+			"amoswap.d" #asm_or " %0, %2, %1"	\
+			: "=r" (__ret), "+A" (*__ptr)		\
+			: "r" (__new));				\
+		break;						\
+	default:						\
+		BUILD_BUG();					\
+	}							\
+	__ret;							\
+})
+
+#define xchg(ptr, x)    (__xchg((x), (ptr), sizeof(*(ptr)), .aqrl))
+
+#define xchg32(ptr, x)				\
+({						\
+	BUILD_BUG_ON(sizeof(*(ptr)) != 4);	\
+	xchg((ptr), (x));			\
+})
+
+#define xchg64(ptr, x)				\
+({						\
+	BUILD_BUG_ON(sizeof(*(ptr)) != 8);	\
+	xchg((ptr), (x));			\
+})
+

arch/riscv/include/asm/cmpxchg.h line 70

Quoted 10 lines
arch/riscv/include/asm/cmpxchg.h new file +10 -0
+/*
+ * Atomic compare and exchange.  Compare OLD with MEM, if identical,
+ * store NEW in MEM.  Return the initial value in MEM.  Success is
+ * indicated by comparing RETURN with OLD.
+ */
+#define __cmpxchg(ptr, old, new, size, lrb, scb)			\
+({									\
+	__typeof__(ptr) __ptr = (ptr);					\
+	__typeof__(old) __old = (old);					\
+	__typeof__(new) __new = (new);					\

Better write those two lines as:

	__typeof__(*(ptr)) __old = (old);					\
	__typeof__(*(ptr)) __new = (new);					\

? I'm thinking the case where @old and @new are int and ptr is "long *", could the asm below do the implicitly converting right, i.e. keep the sign bit?

Regards,
Boqun

Quoted 67 lines
arch/riscv/include/asm/cmpxchg.h new file +67 -0
+	__typeof__(*(ptr)) __ret;					\
+	register unsigned int __rc;					\
+	switch (size) {							\
+	case 4:								\
+		__asm__ __volatile__ (					\
+		"0:"							\
+			"lr.w" #scb " %0, %2\n"				\
+			"bne         %0, %z3, 1f\n"			\
+			"sc.w" #lrb " %1, %z4, %2\n"			\
+			"bnez        %1, 0b\n"				\
+		"1:"							\
+			: "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr)	\
+			: "rJ" (__old), "rJ" (__new));			\
+		break;							\
+	case 8:								\
+		__asm__ __volatile__ (					\
+		"0:"							\
+			"lr.d" #scb " %0, %2\n"				\
+			"bne         %0, %z3, 1f\n"			\
+			"sc.d" #lrb " %1, %z4, %2\n"			\
+			"bnez        %1, 0b\n"				\
+		"1:"							\
+			: "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr)	\
+			: "rJ" (__old), "rJ" (__new));			\
+		break;							\
+	default:							\
+		BUILD_BUG();						\
+	}								\
+	__ret;								\
+})
+
+#define cmpxchg(ptr, o, n) \
+	(__cmpxchg((ptr), (o), (n), sizeof(*(ptr)), .aqrl, .aqrl))
+
+#define cmpxchg_local(ptr, o, n) \
+	(__cmpxchg((ptr), (o), (n), sizeof(*(ptr)), , ))
+
+#define cmpxchg32(ptr, o, n)			\
+({						\
+	BUILD_BUG_ON(sizeof(*(ptr)) != 4);	\
+	cmpxchg((ptr), (o), (n));		\
+})
+
+#define cmpxchg32_local(ptr, o, n)		\
+({						\
+	BUILD_BUG_ON(sizeof(*(ptr)) != 4);	\
+	cmpxchg_local((ptr), (o), (n));		\
+})
+
+#define cmpxchg64(ptr, o, n)			\
+({						\
+	BUILD_BUG_ON(sizeof(*(ptr)) != 8);	\
+	cmpxchg((ptr), (o), (n));		\
+})
+
+#define cmpxchg64_local(ptr, o, n)		\
+({						\
+	BUILD_BUG_ON(sizeof(*(ptr)) != 8);	\
+	cmpxchg_local((ptr), (o), (n));		\
+})
+
+#else /* !CONFIG_ISA_A */
+
+#include <asm-generic/cmpxchg.h>
+
+#endif /* CONFIG_ISA_A */
+

[...]

#12 lore raw

Re: [PATCH 2/9] RISC-V: Atomic and Locking Code

In reply to Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Palmer Dabbelt

+/*
+ * FIXME: I could only find documentation that atomic_{add,sub,inc,dec} are
+ * barrier-free. I'm assuming that and/or/xor have the same constraints as the
+ * others.
+ */

Quoted 8 lines

Yes.. we have new documentation in the work to which I would post a link but for some reason copy/paste stopped working again (Konsole does that at times and is #$%#$%#4# annoying).

Ha, found it using google...

https://marc.info/?l=linux-kernel&m=14972790112580

The link is broken, you miss a tailing 1 ;-)

	https://marc.info/?l=linux-kernel&m=149727901125801

Regards,
Boqun

maintainer
#13 lore raw

Re: [PATCH 2/9] RISC-V: Atomic and Locking Code

Quoted 12 lines · Boqun Feng
Quoted 11 lines · Peter Zijlstra
Quoted 5 lines · Palmer Dabbelt

+/*
+ * FIXME: I could only find documentation that atomic_{add,sub,inc,dec} are
+ * barrier-free. I'm assuming that and/or/xor have the same constraints as the
+ * others.
+ */

Yes.. we have new documentation in the work to which I would post a link but for some reason copy/paste stopped working again (Konsole does that at times and is #$%#$%#4# annoying).

Quoted 9 lines
Quoted 4 lines

Ha, found it using google...

https://marc.info/?l=linux-kernel&m=14972790112580

The link is broken, you miss a tailing 1 ;-)

	https://marc.info/?l=linux-kernel&m=149727901125801

Argh... thanks for fixing that!

#34 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig

I'm a bit concerned about these cmpxchg syscalls, and I'd like to understand if my concerns are justified.

For a new instruction set that starts out in the 201x years we really should have cmpxchg as a mandatory instruction - if not in the CPU it should be in the Linux ABI so that we don't have to deal with a mess where programs will either need an indirection for CPUs that have cmpxchg capabilities vs those that don't, and we don't need to worry if given binaries work on all CPUs.

Agreed on the indirection; it feels like this is something that should be in the vDSO, which could use the cmpxchg instruction if it's available, or otherwise just uses plain loads and stores. In the latter case, the kernel can then detect if you're preempted in the vdso critical region and restart the cmpxchg (we do something similar for arch/arm/, but it's slightly simpler with the vectors page (see entry-armv.S).

Alternatively, we could revisit the restartable sequences work from Mathieu, but I think that's stalled pending real-world performance data.

Quoted 2 lines

What keeps from from declaring that the RISV-A extension is mandatory for Linux?

Or that!

Will

#35 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

Quoted 3 lines · Will Deacon

Agreed on the indirection; it feels like this is something that should be in the vDSO, which could use the cmpxchg instruction if it's available, or otherwise just uses plain loads and stores.

Even that seems like a lot of indirection for something that is in the critical fast path for synchronization. I really can't understand how a new ISA / ABI could even come up with an idea as stupid as making essential synchronization primitives optional.

#43 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

In reply to Palmer Dabbelt
Quoted 36 lines · Palmer Dabbelt
Quoted 27 lines · On Mon, 03 Jul 2017 16:06:39 PDT (-0700), [email protected] wrote:
Quoted 26 lines · Palmer Dabbelt
Quoted 7 lines · On Wed, 28 Jun 2017 15:42:37 PDT (-0700), [email protected] wrote:
Quoted 5 lines · Palmer Dabbelt
arch/riscv/include/uapi/asm/ucontext.h new file +0 -0
new file mode 100644
index 000000000000..52eff9febcfd

...

Quoted 12 lines

arch/riscv/include/uapi/asm/ucontext.h line 33

Quoted 10 lines
arch/riscv/include/uapi/asm/ucontext.h new file +10 -0
+struct ucontext {
+	unsigned long	  uc_flags;
+	struct ucontext	 *uc_link;
+	stack_t		  uc_stack;
+	sigset_t	  uc_sigmask;
+	/* glibc uses a 1024-bit sigset_t */
+	__u8		  __unused[1024 / 8 - sizeof(sigset_t)];
+	/* last for future expansion */
+	struct sigcontext uc_mcontext;
+};

Any particular reason not to use the asm-generic ucontext?

In the generic ucontext, 'uc_sigmask' is at the end of the structure so it can be expanded. Since we want our mcontext to be expandable as well, we pre-allocate some expandable space for sigmask and then put mcontext at the end.

Quoted 7 lines
Quoted 1 line

We stole this idea from arm64.

Curious. __unused seems like overkill to be honest given that expanding the number of signals up to 128 causes other issues (as discovered on MIPS e.g. the waitpid() status, with stopsig not fitting below the exit code (shift 8) and core dump flag (bit 7)), but perhaps it could be carefully expanded by splitting the stopsig field.

Quoted 4 lines

Sorry, I don't understand the intricacies of this in the slightest. In general we try to avoid surprises in software land in RISC-V, so whenever we do something we go look at the most popular architectures (Intel and ARM) and try to ensure we don't paint ourselves into any corners that they didn't.

I think Catalin was concerned that putting uc_sigmask at the end breaks extensibility of sigcontext.

[Catalin, please comment if I'm misquoting you ;) ]

Generic ucontext seems broken in any case, when kernel/user sigset_t sizes differ:

sigset_t oldmask, newmask;

void handler(int n, siginfo_t *si, void *uc_)

{
	ucontext_t *uc = uc_;

	oldmask = uc->uc_sigmask; uc->uc_sigmask = newmask;
}

With generic ucontext, this can overrun and corrupt memory if the user/ kernel sigset_t sizes differ. The only fix is to reserve space in ucontext for the larger of the two sigset sizes, which generic ucontext does not do.

There's also the problem you comment on where only 7 bits of signal number are available in wait() status values: with 0x7f being magic and 0 not a valid signal number, that probably allows up to 126 signals. An arch could possibly have its own definitions to get beyond this, but it's all done by magic numbers and open-coding in core code today.

i.e., the "extensibility" in generic ucontext may be bogus.

So, you can commit to a sane maximum number of signals (say, 64) in your ABI, but this means that your libc sigset_t size probably needs to match and you can never grow beyond that without an ABI break.

Or you can reserve enough space in ucontext for the userspace sigset_t. Using generic signal.h and ucontext.h effectively commits you to max 64 signals AFAICT. The extra space may be permanently wasted, but that's preferable to memory corruption.

(Note, I don't know myself where the "1024" comes from. Are there any POSIXish systems implementing anywhere near that number of signals? Is there a real usecase for it? Maybe it's just overzealous futureproofing?)

Quoted 8 lines
Quoted 3 lines

Looks harmless here I suppose so I defer to others. If it is the preferred approach does it make sense to make it the "default" for new architectures at some point?

Again, this isn't really my thing, but we chose this because we thought it was the sane way to do it. Unless we're doing something silly, I don't see why it wouldn't be a reasonable default. This is predicated on having expandable architectural state, otherwise putting sigmask at the end seems sane.

Note, the contents of sigcontext are nonportable but are nonetheless ABI, and some userspace software does expect to be able to poke about in there, modify the signal return state, etc.

Additionally the whole signal frame cannot safely exceed MINSIGSTKSZ in size (which looks like 2K if you're relying on generic signal.h -- not a big number when compared against upcoming vector architectures).

Going beyond MINSIGSTKSZ is a user ABI break, as is any non-probeable change to the contents of struct sigcontext, including changes to its size.

We are burned by this on arm64 with SVE: arm64 has its own MINSIGSTKSZ (5K), which is not enough for the biggest possible SVE implemetations (over 8K for the SVE state alone). I have some proposals to mitigate this [1], but a complete solution is not possible.

Without any flags, size or version field or some kind of extensible list structure, you would likely have trouble extending your sigcontext without ABI breaks.

The ucontext API is unfortunately fundamentally broken, especially with regard to extensibility. It's debatable whether the context argument to sigaction handlers should have been ucontext_t, but we seem to be stuck with it. POSIX no longer attempts to specify most of the de facto ucontext API behaviour, but it persists because there are things that userspace can't do any other way.

In principle we could cook up a new signal interface, and a new arch could avoid the current and legacy interfaces entirely.

For example, the signal context could be made properly extensible, and a cookie could be registered for each handler so that libc can wrap signal handlers without the need for runtime-generated trampolines. Then a POSIX compatibilty interface could be implemented in libc.

Could be a tough sell though -- and there's a fair risk we'd come up with something that is still broken.

Cheers
---Dave

[1] [RFC PATCH v2 0/6] Signal frame expansion support
http://lists.infradead.org/pipermail/linux-arm-kernel/2017-April/501163.html

(Now merged, except for the AT_MINSIGSTKSZ auxv entry to report the signal frame size -- since we won't need this until later and it could benefit from more discussion and it would be good to build some consensus around it if possible. I plan to talk about this and related topics at Plumbers.)

#36 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Will Deacon

Agreed on the indirection; it feels like this is something that should be in the vDSO, which could use the cmpxchg instruction if it's available, or otherwise just uses plain loads and stores.

Even that seems like a lot of indirection for something that is in the critical fast path for synchronization. I really can't understand how a new ISA / ABI could even come up with an idea as stupid as making essential synchronization primitives optional.

No disagreement there!

Will

author maintainer
#6 lore raw

Re: [patches] [PATCH 1/9] RISC-V: Init and Halt Code

In reply to Jonathan Neuschäfer
Quoted 10 lines · On Tue, 04 Jul 2017 14:54:01 PDT (-0700), [email protected] wrote:

Hi, below are some small comments.

Quoted 3 lines · Palmer Dabbelt

This contains the various __init C functions, the initial assembly kernel entry point, and the code to reset the system. When a file was init-related, it contains

It contains what?

Quoted 1 line
Quoted 8 lines

[...]

arch/riscv/include/asm/bug.h line 24

Quoted 2 lines
arch/riscv/include/asm/bug.h new file +2 -0
+#ifdef CONFIG_GENERIC_BUG
+#define __BUG_INSN	_AC(0x00100073, UL) /* sbreak */

This should be ebreak, not sbreak, in Priv Spec 1.10, AFAICT. I guess binutils still understands sbreak, but it's nicer to stick to the spec, IMHO.

I agree. IIRC they're the same instruction, we just alias sbreak->ebreak in binutils (like scall->ecall).

Quoted 33 lines

arch/riscv/include/asm/bug.h line 51

Quoted 5 lines
arch/riscv/include/asm/bug.h new file +5 -0
+#define BUG()							\
+do {								\
+	__asm__ __volatile__ (					\
+		"1:\n\t"					\
+			"sbreak\n"				\

ebreak

Quoted 6 lines
arch/riscv/include/asm/bug.h new file +6 -0
+			".pushsection __bug_table,\"a\"\n\t"	\
+		"2:\n\t"					\
+			__BUG_ENTRY "\n\t"			\
+			".org 2b + %2\n\t"			\
+			".popsection"				\
+		:						\

arch/riscv/include/asm/bug.h line 67

Quoted 10 lines
arch/riscv/include/asm/bug.h new file +10 -0
+		: "i" (__FILE__), "i" (__LINE__),		\
+		  "i" (sizeof(struct bug_entry)));		\
+	unreachable();						\
+} while (0)
+#endif /* !__ASSEMBLY__ */
+#else /* CONFIG_GENERIC_BUG */
+#ifndef __ASSEMBLY__
+#define BUG()							\
+do {								\
+	__asm__ __volatile__ ("sbreak\n");			\

ebreak

Quoted 4 lines
arch/riscv/include/asm/bug.h new file +4 -0
+	unreachable();						\
+} while (0)
+#endif /* !__ASSEMBLY__ */
+#endif /* CONFIG_GENERIC_BUG */

[...]

Quoted 15 lines

arch/riscv/kernel/traps.c line 115

Quoted 12 lines
arch/riscv/kernel/traps.c new file +12 -0
+#define DO_ERROR_INFO(name, signo, code, str)				\
+asmlinkage void name(struct pt_regs *regs)				\
+{									\
+	do_trap_error(regs, signo, code, regs->sepc, "Oops - " str);	\
+}
+
+DO_ERROR_INFO(do_trap_unknown,
+	SIGILL, ILL_ILLTRP, "unknown exception");
+DO_ERROR_INFO(do_trap_insn_misaligned,
+	SIGBUS, BUS_ADRALN, "instruction address misaligned");
+DO_ERROR_INFO(do_trap_insn_fault,
+	SIGBUS, BUS_ADRALN, "instruction access fault");

For a general instruction access fault, BUS_ADRALN seems wrong. A variant of SIGSEGV seems more appropriate, IMHO.

How does this look?

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 4c693b5b9980..3ce9ac6e736e 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -112,7 +112,7 @@ DO_ERROR_INFO(do_trap_unknown,
DO_ERROR_INFO(do_trap_insn_misaligned,

          SIGBUS, BUS_ADRALN, "instruction address misaligned");
   DO_ERROR_INFO(do_trap_insn_fault,
  -       SIGBUS, BUS_ADRALN, "instruction access fault");
  +       SIGBUS, SEGV_ACCERR, "instruction access fault");
   DO_ERROR_INFO(do_trap_insn_illegal,
          SIGILL, ILL_ILLOPC, "illegal instruction");

DO_ERROR_INFO(do_trap_load_misaligned,

author maintainer
#14 lore raw

Re: [PATCH 2/9] RISC-V: Atomic and Locking Code

In reply to Peter Zijlstra
Quoted 7 lines · On Wed, 05 Jul 2017 01:43:21 PDT (-0700), [email protected] wrote:
Quoted 5 lines · Palmer Dabbelt

+/*
+ * FIXME: I could only find documentation that atomic_{add,sub,inc,dec} are
+ * barrier-free. I'm assuming that and/or/xor have the same constraints as the
+ * others.
+ */

Quoted 7 lines

Yes.. we have new documentation in the work to which I would post a link but for some reason copy/paste stopped working again (Konsole does that at times and is #$%#$%#4# annoying).

Ha, found it using google...

https://marc.info/?l=linux-kernel&m=149727901125801

Thanks.

Quoted 10 lines
Quoted 5 lines

+/*
+ * atomic_{cmp,}xchg is required to have exactly the same ordering semantics as
+ * {cmp,}xchg and the operations that return, so they need a barrier. We just
+ * use the other implementations directly.
+ */

cmpxchg triggers an extra rule; all conditional operations only need to imply barriers on success. So a cmpxchg that fails, need not imply any ordering what so ever.

Quoted 9 lines

arch/riscv/include/asm/barrier.h line 46

Quoted 6 lines
arch/riscv/include/asm/barrier.h new file +6 -0
+/*
+ * Our atomic operations set the AQ and RL bits and therefor we don't need to
+ * fence around atomics.
+ */
+#define __smb_mb__before_atomic()	barrier()
+#define __smb_mb__after_atomic()	barrier()

Ah, not quite... you need full barriers here. Because your regular atomic ops imply no ordering what so ever.

The new documentation helps here, too. Thanks!

  diff --git a/arch/riscv/include/asm/barrier.h b/arch/riscv/include/asm/barrier.h
  index 82a0092a86d0..a480c0fb85e5 100644
  --- a/arch/riscv/include/asm/barrier.h
  +++ b/arch/riscv/include/asm/barrier.h
  @@ -39,11 +39,19 @@
   #define smp_wmb()      RISCV_FENCE(w,w)

/*
- * Our atomic operations set the AQ and RL bits and therefor we don't need to
- * fence around atomics.
+ * These fences exist to enforce ordering around the relaxed AMOs. The
+ * documentation defines that
+ * "
+ * atomic_fetch_add();
+ * is equivalent to:

  + *     smp_mb__before_atomic();
  + *     atomic_fetch_add_relaxed();
  + *     smp_mb__after_atomic();
  + * "
  + * So we emit full fences on both sides.
    */
  -#define __smb_mb__before_atomic()      barrier()
  -#define __smb_mb__after_atomic()       barrier()
  +#define __smb_mb__before_atomic()      smp_mb()
  +#define __smb_mb__after_atomic()       smp_mb()

/*
* These barries are meant to prevent memory operations inside a spinlock from

Quoted 4 lines
Quoted 4 lines

+/*
+ * These barries are meant to prevent memory operations inside a spinlock from
+ * moving outside of that spinlock. Since we set the AQ and RL bits when
+ * entering or leaving spinlocks, no additional fence needs to be performed.

Quoted 8 lines

arch/riscv/include/asm/barrier.h line 54

Quoted 3 lines

+ */

arch/riscv/include/asm/barrier.h new file +2 -0
+#define smb_mb__before_spinlock()	barrier()
+#define smb_mb__after_spinlock()	barrier()

Also probably not true. I _think_ you want a full barrier here, but given the total lack of documentation on your end and the fact I've not yet read the spinlock (which I suppose is below) I cannot yet state more.

Ya, sorry about that -- we're waiting on a proper memory model spec. Is there any other documentation I should produce?

More below.

Quoted 7 lines

arch/riscv/include/asm/barrier.h line 57

Quoted 3 lines
arch/riscv/include/asm/barrier.h new file +3 -0
+
+/* FIXME: I don't think RISC-V is allowed to perform a speculative load. */
+#define smp_acquire__after_ctrl_dep()	barrier()

That would be a very weird thing to disallow... speculative loads are teh awesome ;-) Note you can get the very same effect from caches when your stores are not globally atomic.

OK -- I guess generally the user ISA spec is written disregarding microarchitecture, so I assumed this would be illegal. We'll wait for the memory model spec.

  diff --git a/arch/riscv/include/asm/barrier.h b/arch/riscv/include/asm/barrier.h
  index a4e54f4c17eb..c039333d4a5d 100644
  --- a/arch/riscv/include/asm/barrier.h
  +++ b/arch/riscv/include/asm/barrier.h
  @@ -61,8 +61,12 @@
   #define smb_mb__before_spinlock()      smp_mb()
   #define smb_mb__after_spinlock()       smp_mb()

-/* FIXME: I don't think RISC-V is allowed to perform a speculative load. */ -#define smp_acquire__after_ctrl_dep() barrier()
+/*
+ * TODO_RISCV_MEMORY_MODEL: I don't think RISC-V is allowed to perform a
+ * speculative load, but we're going to wait on a formal memory model in order
+ * to ensure this is safe to elide.
+ */
+#define smp_acquire__after_ctrl_dep() smp_mb()

   /*
    * The RISC-V ISA doesn't support byte or half-word AMOs, so we fall back to a
  @@ -137,24 +141,6 @@
          __u.__val;                                                      \
   })

-/*
- * The default implementation of this uses READ_ONCE and
- * smp_acquire__after_ctrl_dep, but since we can directly do an ACQUIRE load we
- * can avoid the extra barrier.
- */

  -#define smp_cond_load_acquire(ptr, cond_expr) ({                       \
  -       typeof(ptr) __PTR = (ptr);                                      \
  -       typeof(*ptr) VAL;                                               \
  -       for (;;) {                                                      \
  -               VAL = __smp_load_acquire(__PTR);                        \
  -               if (cond_expr)                                          \
  -                       break;                                          \
  -               cpu_relax();                                            \
  -       }                                                               \
  -       smp_acquire__after_ctrl_dep();                                  \
  -       VAL;                                                            \

-})
-
#include <asm-generic/barrier.h>

   #endif /* __ASSEMBLY__ */
Quoted 64 lines
Quoted 51 lines
arch/riscv/include/asm/barrier.h new file +51 -0
+/*
+ * The RISC-V ISA doesn't support byte or half-word AMOs, so we fall back to a
+ * regular store and a fence here.  Otherwise we emit an AMO with an AQ or RL
+ * bit set and allow the microarchitecture to avoid the other half of the AMO.
+ */
+#define __smp_store_release(p, v)					\
+do {									\
+	union { typeof(*p) __val; char __c[1]; } __u =			\
+		{ .__val = (__force typeof(*p)) (v) };			\
+	compiletime_assert_atomic_type(*p);				\
+	switch (sizeof(*p)) {						\
+	case 1:								\
+	case 2:								\
+		smb_mb();						\
+		WRITE_ONCE(*p, __u.__val);				\
+		break;							\
+	case 4:								\
+		__asm__ __volatile__ (					\
+			"amoswap.w.rl zero, %1, %0"			\
+			: "+A" (*p), "r" (__u.__val)			\
+			: 						\
+			: "memory");					\
+		break;							\
+	case 8:								\
+		__asm__ __volatile__ (					\
+			"amoswap.d.rl zero, %1, %0"			\
+			: "+A" (*p), "r" (__u.__val)			\
+			: 						\
+			: "memory");					\
+		break;							\
+	}								\
+} while (0)
+
+#define __smp_load_acquire(p)						\
+do {									\
+	union { typeof(*p) __val; char __c[1]; } __u =			\
+		{ .__val = (__force typeof(*p)) (v) };			\
+	compiletime_assert_atomic_type(*p);				\
+	switch (sizeof(*p)) {						\
+	case 1:								\
+	case 2:								\
+		__u.__val = READ_ONCE(*p);				\
+		smb_mb();						\
+		break;							\
+	case 4:								\
+		__asm__ __volatile__ (					\
+			"amoor.w.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\

arch/riscv/include/asm/barrier.h line 119

Quoted 10 lines
arch/riscv/include/asm/barrier.h new file +10 -0
+	case 8:								\
+		__asm__ __volatile__ (					\
+			"amoor.d.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\
+	}								\
+	__u.__val;							\
+} while (0)

'creative' use of amoswap and amoor :-)

Quoted 3 lines

You should really look at a normal load with ordering instruction though, that amoor.aq is a rmw and will promote the cacheline to exclusive (and dirty it).

The thought here was that implementations could elide the MW by pattern matching the "zero" (x0, the architectural zero register) forms of AMOs where it's interesting. I talked to one of our microarchitecture guys, and while he agrees that's easy he points out that eliding half the AMO may wreak havoc on the consistency model. Since we're not sure what the memory model is actually going to look like, we thought it'd be best to just write the simplest code here

  /*
   * TODO_RISCV_MEMORY_MODEL: While we could emit AMOs for the W and D sized
   * accesses here, it's questionable if that actually helps or not: the lack of
   * offsets in the AMOs means they're usually preceded by an addi, so they
   * probably won't save code space.  For now we'll just emit the fence.
   */
  #define __smp_store_release(p, v)                                       \
  ({                                                                      \
          compiletime_assert_atomic_type(*p);                             \
          smp_mb();                                                       \
          WRITE_ONCE(*p, v);                                              \
  })

  #define __smp_load_acquire(p)                                           \
  ({                                                                      \
          union{typeof(*p) __p; long __l;} __u;                           \
          compiletime_assert_atomic_type(*p);                             \
          __u.__l = READ_ONCE(*p);                                        \
          smp_mb();                                                       \
          __u.__p;                                                        \
  })
Quoted 60 lines
Quoted 32 lines
arch/riscv/include/asm/spinlock.h new file +32 -0
+/*
+ * Simple spin lock operations.  These provide no fairness guarantees.
+ */
+
+/* FIXME: Replace this with a ticket lock, like MIPS. */
+
+#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
+#define arch_spin_is_locked(x)	((x)->lock != 0)
+#define arch_spin_unlock_wait(x) \
+		do { cpu_relax(); } while ((x)->lock)
+
+static inline void arch_spin_unlock(arch_spinlock_t *lock)
+{
+	__asm__ __volatile__ (
+		"amoswap.w.rl x0, x0, %0"
+		: "=A" (lock->lock)
+		:: "memory");
+}
+
+static inline int arch_spin_trylock(arch_spinlock_t *lock)
+{
+	int tmp = 1, busy;
+
+	__asm__ __volatile__ (
+		"amoswap.w.aq %0, %2, %1"
+		: "=r" (busy), "+A" (lock->lock)
+		: "r" (tmp)
+		: "memory");
+
+	return !busy;
+}
+

arch/riscv/include/asm/spinlock.h line 62

Quoted 10 lines
arch/riscv/include/asm/spinlock.h new file +10 -0
+static inline void arch_spin_lock(arch_spinlock_t *lock)
+{
+	while (1) {
+		if (arch_spin_is_locked(lock))
+			continue;
+
+		if (arch_spin_trylock(lock))
+			break;
+	}
+}

OK, so back to smp_mb__{before,after}_spinlock(), that wants to order things like:

	wakeup:					block:

	COND = 1;				p->state = UNINTERRUPTIBLE;
						smp_mb();
	smp_mb__before_spinlock();
	spin_lock(&lock);			if (!COND)
						  schedule()
	if (p->state & state)
		goto out;

And here it is important that the COND store not happen _after_ the p->state load.

Quoted 7 lines

Now, your spin_lock() only implies the AQ thing, which should only constraint later load/stores but does nothing for the prior load/stores. So our COND store can drop into the lock and even happen after the p->state load.

So you very much want your smp_mb__{before,after}_spinlock thingies to be full barriers.

OK, thanks! I just had the movement direction backwards. This makes much more sense.

  diff --git a/arch/riscv/include/asm/barrier.h b/arch/riscv/include/asm/barrier.h
  index a480c0fb85e5..a4e54f4c17eb 100644
  --- a/arch/riscv/include/asm/barrier.h
  +++ b/arch/riscv/include/asm/barrier.h
  @@ -54,12 +54,12 @@
   #define __smb_mb__after_atomic()       smp_mb()

/*
- * These barries are meant to prevent memory operations inside a spinlock from
- * moving outside of that spinlock. Since we set the AQ and RL bits when
- * entering or leaving spinlocks, no additional fence needs to be performed.
+ * These barries prevent accesses performed outside a spinlock from being moved
+ * inside a spinlock. Since RISC-V sets the aq/rl bits on our spinlock only
+ * enforce release consistency, we need full fences here.

    */
  -#define smb_mb__before_spinlock()      barrier()
  -#define smb_mb__after_spinlock()       barrier()
  +#define smb_mb__before_spinlock()      smp_mb()
  +#define smb_mb__after_spinlock()       smp_mb()

/* FIXME: I don't think RISC-V is allowed to perform a speculative load. */ #define smp_acquire__after_ctrl_dep() barrier()

Thanks for all the help!

#15 lore raw

Re: [PATCH 2/9] RISC-V: Atomic and Locking Code

On Thu, Jul 06, 2017 at 06:04:13PM -0700, Palmer Dabbelt wrote:
[...]

Quoted 43 lines
Quoted 31 lines
Quoted 18 lines
arch/riscv/include/asm/barrier.h new file +18 -0
+#define __smp_load_acquire(p)						\
+do {									\
+	union { typeof(*p) __val; char __c[1]; } __u =			\
+		{ .__val = (__force typeof(*p)) (v) };			\
+	compiletime_assert_atomic_type(*p);				\
+	switch (sizeof(*p)) {						\
+	case 1:								\
+	case 2:								\
+		__u.__val = READ_ONCE(*p);				\
+		smb_mb();						\
+		break;							\
+	case 4:								\
+		__asm__ __volatile__ (					\
+			"amoor.w.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\

arch/riscv/include/asm/barrier.h line 119

Quoted 10 lines
arch/riscv/include/asm/barrier.h new file +10 -0
+	case 8:								\
+		__asm__ __volatile__ (					\
+			"amoor.d.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\
+	}								\
+	__u.__val;							\
+} while (0)

'creative' use of amoswap and amoor :-)

Quoted 3 lines

You should really look at a normal load with ordering instruction though, that amoor.aq is a rmw and will promote the cacheline to exclusive (and dirty it).

The thought here was that implementations could elide the MW by pattern matching the "zero" (x0, the architectural zero register) forms of AMOs where it's interesting. I talked to one of our microarchitecture guys, and while he agrees that's easy he points out that eliding half the AMO may wreak havoc on the consistency model. Since we're not sure what the memory model is actually going to look like, we thought it'd be best to just write the simplest code here

Quoted 16 lines
  /*
   * TODO_RISCV_MEMORY_MODEL: While we could emit AMOs for the W and D sized
   * accesses here, it's questionable if that actually helps or not: the lack of
   * offsets in the AMOs means they're usually preceded by an addi, so they
   * probably won't save code space.  For now we'll just emit the fence.
   */
  #define __smp_store_release(p, v)                                       \
  ({                                                                      \
          compiletime_assert_atomic_type(*p);                             \
          smp_mb();                                                       \
          WRITE_ONCE(*p, v);                                              \
  })

  #define __smp_load_acquire(p)                                           \
  ({                                                                      \
          union{typeof(*p) __p; long __l;} __u;                           \

AFAICT, there seems to be an endian issue if you do this. No?

Let us assume typeof(*p) is char and *p == 1, and on a big endian 32bit platform:

Quoted 2 lines
          compiletime_assert_atomic_type(*p);                             \
          __u.__l = READ_ONCE(*p);                                        \
	READ_ONCE(*p) is 1 so
	__u.__l is 0x00 00 00 01 now
Quoted 2 lines
          smp_mb();                                                       \
          __u.__p;                                                        \
	__u.__p is then 0x00.

Am I missing something here?

Even so why not use the simple definition as in include/asm-generic/barrier.h?

Regards,
Boqun

Quoted 2 lines
  })

[...]

maintainer
#17 lore raw

Re: [PATCH 2/9] RISC-V: Atomic and Locking Code

In reply to Palmer Dabbelt
Quoted 7 lines · Palmer Dabbelt
Quoted 4 lines

Also probably not true. I _think_ you want a full barrier here, but given the total lack of documentation on your end and the fact I've not yet read the spinlock (which I suppose is below) I cannot yet state more.

Ya, sorry about that -- we're waiting on a proper memory model spec. Is there any other documentation I should produce?

Nah, I'll wait for your shiny new document.

Quoted 6 lines

+/*
+ * TODO_RISCV_MEMORY_MODEL: I don't think RISC-V is allowed to perform a
+ * speculative load, but we're going to wait on a formal memory model in order
+ * to ensure this is safe to elide.
+ */
+#define smp_acquire__after_ctrl_dep() smp_mb()

So typically a control dependency already provides read->write ordering, by virtue of speculative writes being BAD.

So a control dependency only needs to provide read->read ordering in addition to the existing read->write ordering and hence this barrier is typically a smp_rmb().

See the definition in asm-generic/barrier.h.

Having to use a full barrier here would imply your architecture does not respect control dependencies, which would be BAD because we actually rely on them.

So either the normal definition is good and you don't need to do anything, or you prohibit read speculation in which case you have a special case like TILE does.

Quoted 56 lines
Quoted 31 lines
Quoted 18 lines
arch/riscv/include/asm/barrier.h new file +18 -0
+#define __smp_load_acquire(p)						\
+do {									\
+	union { typeof(*p) __val; char __c[1]; } __u =			\
+		{ .__val = (__force typeof(*p)) (v) };			\
+	compiletime_assert_atomic_type(*p);				\
+	switch (sizeof(*p)) {						\
+	case 1:								\
+	case 2:								\
+		__u.__val = READ_ONCE(*p);				\
+		smb_mb();						\
+		break;							\
+	case 4:								\
+		__asm__ __volatile__ (					\
+			"amoor.w.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\

arch/riscv/include/asm/barrier.h line 119

Quoted 10 lines
arch/riscv/include/asm/barrier.h new file +10 -0
+	case 8:								\
+		__asm__ __volatile__ (					\
+			"amoor.d.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\
+	}								\
+	__u.__val;							\
+} while (0)

'creative' use of amoswap and amoor :-)

Quoted 3 lines

You should really look at a normal load with ordering instruction though, that amoor.aq is a rmw and will promote the cacheline to exclusive (and dirty it).

The thought here was that implementations could elide the MW by pattern matching the "zero" (x0, the architectural zero register) forms of AMOs where it's interesting. I talked to one of our microarchitecture guys, and while he agrees that's easy he points out that eliding half the AMO may wreak havoc on the consistency model. Since we're not sure what the memory model is actually going to look like, we thought it'd be best to just write the simplest code here

  /*
   * TODO_RISCV_MEMORY_MODEL: While we could emit AMOs for the W and D sized
   * accesses here, it's questionable if that actually helps or not: the lack of
   * offsets in the AMOs means they're usually preceded by an addi, so they
   * probably won't save code space.  For now we'll just emit the fence.
   */
  #define __smp_store_release(p, v)                                       \
  ({                                                                      \
          compiletime_assert_atomic_type(*p);                             \
          smp_mb();                                                       \
          WRITE_ONCE(*p, v);                                              \
  })
Quoted 8 lines
  #define __smp_load_acquire(p)                                           \
  ({                                                                      \
          union{typeof(*p) __p; long __l;} __u;                           \
          compiletime_assert_atomic_type(*p);                             \
          __u.__l = READ_ONCE(*p);                                        \
          smp_mb();                                                       \
          __u.__p;                                                        \
  })

Fair enough, that works.

Quoted 35 lines
Quoted 17 lines

OK, so back to smp_mb__{before,after}_spinlock(), that wants to order things like:

	wakeup:					block:

	COND = 1;				p->state = UNINTERRUPTIBLE;
						smp_mb();
	smp_mb__before_spinlock();
	spin_lock(&lock);			if (!COND)
						  schedule()
	if (p->state & state)
		goto out;

And here it is important that the COND store not happen _after_ the p->state load.

Quoted 7 lines

Now, your spin_lock() only implies the AQ thing, which should only constraint later load/stores but does nothing for the prior load/stores. So our COND store can drop into the lock and even happen after the p->state load.

So you very much want your smp_mb__{before,after}_spinlock thingies to be full barriers.

OK, thanks! I just had the movement direction backwards. This makes much more sense.

  diff --git a/arch/riscv/include/asm/barrier.h b/arch/riscv/include/asm/barrier.h
  index a480c0fb85e5..a4e54f4c17eb 100644
  --- a/arch/riscv/include/asm/barrier.h
  +++ b/arch/riscv/include/asm/barrier.h
  @@ -54,12 +54,12 @@
   #define __smb_mb__after_atomic()       smp_mb()
Quoted 13 lines
   /*
  - * These barries are meant to prevent memory operations inside a spinlock from
  - * moving outside of that spinlock.  Since we set the AQ and RL bits when
  - * entering or leaving spinlocks, no additional fence needs to be performed.
  + * These barries prevent accesses performed outside a spinlock from being moved
  + * inside a spinlock.  Since RISC-V sets the aq/rl bits on our spinlock only
  + * enforce release consistency, we need full fences here.
    */
  -#define smb_mb__before_spinlock()      barrier()
  -#define smb_mb__after_spinlock()       barrier()
  +#define smb_mb__before_spinlock()      smp_mb()
  +#define smb_mb__after_spinlock()       smp_mb()

Most excellent. Thanks!

#7 lore raw

Re: [patches] [PATCH 1/9] RISC-V: Init and Halt Code

In reply to Palmer Dabbelt
Quoted 1 line · Palmer Dabbelt

On Tue, 04 Jul 2017 14:54:01 PDT (-0700), [email protected] wrote:

[...]

Quoted 24 lines
Quoted 7 lines
Quoted 7 lines
arch/riscv/kernel/traps.c new file +7 -0
+#define DO_ERROR_INFO(name, signo, code, str)				\
+asmlinkage void name(struct pt_regs *regs)				\
+{									\
+	do_trap_error(regs, signo, code, regs->sepc, "Oops - " str);	\
+}
+
+DO_ERROR_INFO(do_trap_unknown,
Quoted 8 lines

arch/riscv/kernel/traps.c line 115

Quoted 5 lines
arch/riscv/kernel/traps.c new file +5 -0
+	SIGILL, ILL_ILLTRP, "unknown exception");
+DO_ERROR_INFO(do_trap_insn_misaligned,
+	SIGBUS, BUS_ADRALN, "instruction address misaligned");
+DO_ERROR_INFO(do_trap_insn_fault,
+	SIGBUS, BUS_ADRALN, "instruction access fault");

For a general instruction access fault, BUS_ADRALN seems wrong. A variant of SIGSEGV seems more appropriate, IMHO.

How does this look?

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 4c693b5b9980..3ce9ac6e736e 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -112,7 +112,7 @@ DO_ERROR_INFO(do_trap_unknown,
DO_ERROR_INFO(do_trap_insn_misaligned,

Quoted 7 lines
          SIGBUS, BUS_ADRALN, "instruction address misaligned");
   DO_ERROR_INFO(do_trap_insn_fault,
  -       SIGBUS, BUS_ADRALN, "instruction access fault");
  +       SIGBUS, SEGV_ACCERR, "instruction access fault");
   DO_ERROR_INFO(do_trap_insn_illegal,
          SIGILL, ILL_ILLOPC, "illegal instruction");
   DO_ERROR_INFO(do_trap_load_misaligned,

I'm not familiar with the trap handling infrastructure, but looking at include/uapi/asm-generic/siginfo.h, SEGV_ACCERR would alias to BUS_ADRERR (both are defined as (__SI_FAULT|2)). So if you use SEGV_*, you need to use SIGSEGV, too.

With DO_ERROR_INFO(..., SIGSEGV, SEGV_ACCERR, ...); it looks good to me.

Jonathan Neuschäfer

#20 lore raw

Re: [patches] [PATCH 2/9] RISC-V: Atomic and Locking Code

In reply to Palmer Dabbelt

On Tue, Jul 04, 2017 at 12:50:55PM -0700, Palmer Dabbelt wrote:
[...]

Quoted 1 line

+/* These barries need to enforce ordering on both devices or memory. */

Very minor nit: s/barries/barriers/ (in several places)

Jonathan Neuschäfer

author maintainer
#38 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

In reply to a message not in the archive
Quoted 53 lines · Palmer Dabbelt
Quoted 5 lines · On Thu, 06 Jul 2017 08:45:13 PDT (-0700), [email protected] wrote:
Quoted 4 lines · Christoph Hellwig
Quoted 3 lines · Will Deacon

Agreed on the indirection; it feels like this is something that should be in the vDSO, which could use the cmpxchg instruction if it's available, or otherwise just uses plain loads and stores.

These are already in the vDSO, and use the corresponding atomic instructions on systems with the A extension. The vDSO routines call the system calls in non-A systems. As far as I can tell that's necessary to preserve atomicity, which we currently do by disabling scheduling. If there's a way to do this without entering the kernel then I'd be happy to support it, but I'm not sure how we could maintain atomicity using only regular loads and stores.

Quoted 6 lines
Quoted 4 lines

Even that seems like a lot of indirection for something that is in the critical fast path for synchronization. I really can't understand how a new ISA / ABI could even come up with an idea as stupid as making essential synchronization primitives optional.

No disagreement there!

The default set of multilibs on Linux are:

* rv32imac: 32-bit; Multiply, Atomic, and Compressed extensions
* rv32imafdc: like above, but with single+double float
* rv64imac: 64-bit, Multiply, Atomic and Compressed
* rv64imafdc: like above, but with single+double float

all of which support the A extension. We certainly don't plan on building any systems that support Linux without the A extension at SiFive, so I'm fine removing the system call -- this was originally added by a user, so there was at least enough interest for someone to add the system call.

We've found people are retrofitting other cores to run RISC-V, and I could certainly imagine an older design that lacks a beefy enough memory system to support our atomics (which are LR/SC based) being a design that might arise. There's a lot of systems where people don't seem to care that much about the performance and just want something to work -- if they're on such a tiny system they can't implement the A extension then they're probably not going to be doing a lot of atomics anyway, so maybe it doesn't matter if atomics are slow. As the cost for supporting these A-less systems seems fairly small, it seemed like the right thing to do -- one of the points of making RISC-V have many optional extensions was to let people pick the ones they view as important. Since I don't know the performance constraints of their systems or the cost of implementing the A extension in their design, I'm not really qualified to tell them a cmpxchg syscall is a bad idea.

I'm fine either way here: if someone's core can't support the A extension they can always just buy one that does (ideally from us :)). If it was up to be I'd leave the calls in there, as I generally don't like to tell users we won't support their use case, but since you guys seem to know a lot more about this than I do I'll just leave the decision up to you.

Quoted 5 lines

If you want the system call (and the corresponding vDSO entry, which will be unnecessary if we mandate A) gone then I'll remove it for our v5. Just give me a heads up.

Thanks, and sorry for wasting your time!

I mangled this message when sending it so I'm trying again.

author maintainer
#16 lore raw

Re: [PATCH 2/9] RISC-V: Atomic and Locking Code

In reply to Boqun Feng
Quoted 73 lines · On Thu, 06 Jul 2017 19:14:25 PDT (-0700), [email protected] wrote:

On Thu, Jul 06, 2017 at 06:04:13PM -0700, Palmer Dabbelt wrote:
[...]

Quoted 43 lines
Quoted 31 lines
Quoted 18 lines
arch/riscv/include/asm/barrier.h new file +18 -0
+#define __smp_load_acquire(p)						\
+do {									\
+	union { typeof(*p) __val; char __c[1]; } __u =			\
+		{ .__val = (__force typeof(*p)) (v) };			\
+	compiletime_assert_atomic_type(*p);				\
+	switch (sizeof(*p)) {						\
+	case 1:								\
+	case 2:								\
+		__u.__val = READ_ONCE(*p);				\
+		smb_mb();						\
+		break;							\
+	case 4:								\
+		__asm__ __volatile__ (					\
+			"amoor.w.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\

arch/riscv/include/asm/barrier.h line 119

Quoted 10 lines
arch/riscv/include/asm/barrier.h new file +10 -0
+	case 8:								\
+		__asm__ __volatile__ (					\
+			"amoor.d.aq %1, zero, %0"			\
+			: "+A" (*p)					\
+			: "=r" (__u.__val)				\
+			: "memory");					\
+		break;							\
+	}								\
+	__u.__val;							\
+} while (0)

'creative' use of amoswap and amoor :-)

Quoted 3 lines

You should really look at a normal load with ordering instruction though, that amoor.aq is a rmw and will promote the cacheline to exclusive (and dirty it).

The thought here was that implementations could elide the MW by pattern matching the "zero" (x0, the architectural zero register) forms of AMOs where it's interesting. I talked to one of our microarchitecture guys, and while he agrees that's easy he points out that eliding half the AMO may wreak havoc on the consistency model. Since we're not sure what the memory model is actually going to look like, we thought it'd be best to just write the simplest code here

Quoted 16 lines
  /*
   * TODO_RISCV_MEMORY_MODEL: While we could emit AMOs for the W and D sized
   * accesses here, it's questionable if that actually helps or not: the lack of
   * offsets in the AMOs means they're usually preceded by an addi, so they
   * probably won't save code space.  For now we'll just emit the fence.
   */
  #define __smp_store_release(p, v)                                       \
  ({                                                                      \
          compiletime_assert_atomic_type(*p);                             \
          smp_mb();                                                       \
          WRITE_ONCE(*p, v);                                              \
  })

  #define __smp_load_acquire(p)                                           \
  ({                                                                      \
          union{typeof(*p) __p; long __l;} __u;                           \

AFAICT, there seems to be an endian issue if you do this. No?

Let us assume typeof(*p) is char and *p == 1, and on a big endian 32bit platform:

Quoted 2 lines
          compiletime_assert_atomic_type(*p);                             \
          __u.__l = READ_ONCE(*p);                                        \
	READ_ONCE(*p) is 1 so
	__u.__l is 0x00 00 00 01 now
Quoted 6 lines
Quoted 2 lines
          smp_mb();                                                       \
          __u.__p;                                                        \
	__u.__p is then 0x00.

Am I missing something here?

We're little endian (though I might have still screwed it up). I didn't really bother looking because...

Quoted 1 line

Even so why not use the simple definition as in include/asm-generic/barrier.h?

...that's much better -- I forgot there were generic versions, as we used to have a much more complicated one.

https://github.com/riscv/riscv-linux/commit/910d2bf4c3c349b670a1d839462e32e122ac70a5

Thanks!

author maintainer
#18 lore raw

Re: [PATCH 2/9] RISC-V: Atomic and Locking Code

In reply to Peter Zijlstra
Quoted 17 lines · On Fri, 07 Jul 2017 01:08:19 PDT (-0700), [email protected] wrote:
Quoted 6 lines · Palmer Dabbelt

+/*
+ * TODO_RISCV_MEMORY_MODEL: I don't think RISC-V is allowed to perform a
+ * speculative load, but we're going to wait on a formal memory model in order
+ * to ensure this is safe to elide.
+ */
+#define smp_acquire__after_ctrl_dep() smp_mb()

So typically a control dependency already provides read->write ordering, by virtue of speculative writes being BAD.

So a control dependency only needs to provide read->read ordering in addition to the existing read->write ordering and hence this barrier is typically a smp_rmb().

See the definition in asm-generic/barrier.h.

Quoted 7 lines

Having to use a full barrier here would imply your architecture does not respect control dependencies, which would be BAD because we actually rely on them.

So either the normal definition is good and you don't need to do anything, or you prohibit read speculation in which case you have a special case like TILE does.

I'd be very surprised (and very unhappy) if we ended up with speculative writes, as that would be a huge mess.

Thanks!

author maintainer
#8 lore raw

Re: [patches] [PATCH 1/9] RISC-V: Init and Halt Code

In reply to Jonathan Neuschäfer
Quoted 35 lines · On Fri, 07 Jul 2017 05:58:55 PDT (-0700), [email protected] wrote:
Quoted 1 line · Palmer Dabbelt

On Tue, 04 Jul 2017 14:54:01 PDT (-0700), [email protected] wrote:

[...]

Quoted 24 lines
Quoted 7 lines
Quoted 7 lines
arch/riscv/kernel/traps.c new file +7 -0
+#define DO_ERROR_INFO(name, signo, code, str)				\
+asmlinkage void name(struct pt_regs *regs)				\
+{									\
+	do_trap_error(regs, signo, code, regs->sepc, "Oops - " str);	\
+}
+
+DO_ERROR_INFO(do_trap_unknown,
Quoted 8 lines

arch/riscv/kernel/traps.c line 115

Quoted 5 lines
arch/riscv/kernel/traps.c new file +5 -0
+	SIGILL, ILL_ILLTRP, "unknown exception");
+DO_ERROR_INFO(do_trap_insn_misaligned,
+	SIGBUS, BUS_ADRALN, "instruction address misaligned");
+DO_ERROR_INFO(do_trap_insn_fault,
+	SIGBUS, BUS_ADRALN, "instruction access fault");

For a general instruction access fault, BUS_ADRALN seems wrong. A variant of SIGSEGV seems more appropriate, IMHO.

How does this look?

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 4c693b5b9980..3ce9ac6e736e 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -112,7 +112,7 @@ DO_ERROR_INFO(do_trap_unknown,
DO_ERROR_INFO(do_trap_insn_misaligned,

Quoted 7 lines
          SIGBUS, BUS_ADRALN, "instruction address misaligned");
   DO_ERROR_INFO(do_trap_insn_fault,
  -       SIGBUS, BUS_ADRALN, "instruction access fault");
  +       SIGBUS, SEGV_ACCERR, "instruction access fault");
   DO_ERROR_INFO(do_trap_insn_illegal,
          SIGILL, ILL_ILLOPC, "illegal instruction");
   DO_ERROR_INFO(do_trap_load_misaligned,
Quoted 6 lines

I'm not familiar with the trap handling infrastructure, but looking at include/uapi/asm-generic/siginfo.h, SEGV_ACCERR would alias to BUS_ADRERR (both are defined as (__SI_FAULT|2)). So if you use SEGV_*, you need to use SIGSEGV, too.

With DO_ERROR_INFO(..., SIGSEGV, SEGV_ACCERR, ...); it looks good to me.

Oh, sorry, I wasn't paying attention. It should be fixed now.

Thanks!

author maintainer
#21 lore raw

Re: [patches] [PATCH 2/9] RISC-V: Atomic and Locking Code

In reply to Jonathan Neuschäfer
Quoted 5 lines · On Fri, 07 Jul 2017 06:16:07 PDT (-0700), [email protected] wrote:

On Tue, Jul 04, 2017 at 12:50:55PM -0700, Palmer Dabbelt wrote:
[...]

Quoted 1 line

+/* These barries need to enforce ordering on both devices or memory. */

Very minor nit: s/barries/barriers/ (in several places)

I think this should do it

https://github.com/riscv/riscv-linux/commit/b356e7a2223b5e21df424ea7e9900e5bf408762f

Thanks!

#39 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

In reply to a message not in the archive
Quoted 7 lines · Palmer Dabbelt
Quoted 5 lines · On Thu, 06 Jul 2017 08:45:13 PDT (-0700), [email protected] wrote:
Quoted 4 lines · Christoph Hellwig
Quoted 3 lines · Will Deacon

Agreed on the indirection; it feels like this is something that should be in the vDSO, which could use the cmpxchg instruction if it's available, or otherwise just uses plain loads and stores.

Quoted 6 lines

These are already in the vDSO, and use the corresponding atomic instructions on systems with the A extension. The vDSO routines call the system calls in non-A systems. As far as I can tell that's necessary to preserve atomicity, which we currently do by disabling scheduling. If there's a way to do this without entering the kernel then I'd be happy to support it, but I'm not sure how we could maintain atomicity using only regular loads and stores.

Take a look at the ARM code I mentioned. You can do away with the syscall if you notice that you preempt a thread inside the critical section of the vDSO, and, in that case you resume execution at a known "restart" address.

Quoted 19 lines
Quoted 6 lines
Quoted 4 lines

Even that seems like a lot of indirection for something that is in the critical fast path for synchronization. I really can't understand how a new ISA / ABI could even come up with an idea as stupid as making essential synchronization primitives optional.

No disagreement there!

The default set of multilibs on Linux are:

* rv32imac: 32-bit; Multiply, Atomic, and Compressed extensions
* rv32imafdc: like above, but with single+double float
* rv64imac: 64-bit, Multiply, Atomic and Compressed
* rv64imafdc: like above, but with single+double float

all of which support the A extension. We certainly don't plan on building any systems that support Linux without the A extension at SiFive, so I'm fine removing the system call -- this was originally added by a user, so there was at least enough interest for someone to add the system call.

Quoted 13 lines

We've found people are retrofitting other cores to run RISC-V, and I could certainly imagine an older design that lacks a beefy enough memory system to support our atomics (which are LR/SC based) being a design that might arise. There's a lot of systems where people don't seem to care that much about the performance and just want something to work -- if they're on such a tiny system they can't implement the A extension then they're probably not going to be doing a lot of atomics anyway, so maybe it doesn't matter if atomics are slow. As the cost for supporting these A-less systems seems fairly small, it seemed like the right thing to do -- one of the points of making RISC-V have many optional extensions was to let people pick the ones they view as important. Since I don't know the performance constraints of their systems or the cost of implementing the A extension in their design, I'm not really qualified to tell them a cmpxchg syscall is a bad idea.

The problem is that by supporting these hypothetical designs that can't do atomics, you hurt sensible designs that *can* do the atomics because you force them to take an additional indirection that could otherwise be avoided.

Will

#40 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

Quoted 4 lines · Will Deacon

The problem is that by supporting these hypothetical designs that can't do atomics, you hurt sensible designs that *can* do the atomics because you force them to take an additional indirection that could otherwise be avoided.

Agreed. But the new patchset seems to remove it already, so I guess we're fine on the kernel side. Now we just need to make sure the glibc API doesn't use any indirections.

Note that it might make sense to emit these for very low end nommu designs. Maybe even running Linux, but in that case they'll just need a special non-standard ABI for very limited use cases.

author maintainer
#42 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

In reply to Will Deacon
Quoted 52 lines · On Tue, 11 Jul 2017 06:22:15 PDT (-0700), [email protected] wrote:
Quoted 7 lines · Palmer Dabbelt
Quoted 5 lines · On Thu, 06 Jul 2017 08:45:13 PDT (-0700), [email protected] wrote:
Quoted 4 lines · Christoph Hellwig
Quoted 3 lines · Will Deacon

Agreed on the indirection; it feels like this is something that should be in the vDSO, which could use the cmpxchg instruction if it's available, or otherwise just uses plain loads and stores.

Quoted 6 lines

These are already in the vDSO, and use the corresponding atomic instructions on systems with the A extension. The vDSO routines call the system calls in non-A systems. As far as I can tell that's necessary to preserve atomicity, which we currently do by disabling scheduling. If there's a way to do this without entering the kernel then I'd be happy to support it, but I'm not sure how we could maintain atomicity using only regular loads and stores.

Take a look at the ARM code I mentioned. You can do away with the syscall if you notice that you preempt a thread inside the critical section of the vDSO, and, in that case you resume execution at a known "restart" address.

Quoted 19 lines
Quoted 6 lines
Quoted 4 lines

Even that seems like a lot of indirection for something that is in the critical fast path for synchronization. I really can't understand how a new ISA / ABI could even come up with an idea as stupid as making essential synchronization primitives optional.

No disagreement there!

The default set of multilibs on Linux are:

* rv32imac: 32-bit; Multiply, Atomic, and Compressed extensions
* rv32imafdc: like above, but with single+double float
* rv64imac: 64-bit, Multiply, Atomic and Compressed
* rv64imafdc: like above, but with single+double float

all of which support the A extension. We certainly don't plan on building any systems that support Linux without the A extension at SiFive, so I'm fine removing the system call -- this was originally added by a user, so there was at least enough interest for someone to add the system call.

Quoted 13 lines

We've found people are retrofitting other cores to run RISC-V, and I could certainly imagine an older design that lacks a beefy enough memory system to support our atomics (which are LR/SC based) being a design that might arise. There's a lot of systems where people don't seem to care that much about the performance and just want something to work -- if they're on such a tiny system they can't implement the A extension then they're probably not going to be doing a lot of atomics anyway, so maybe it doesn't matter if atomics are slow. As the cost for supporting these A-less systems seems fairly small, it seemed like the right thing to do -- one of the points of making RISC-V have many optional extensions was to let people pick the ones they view as important. Since I don't know the performance constraints of their systems or the cost of implementing the A extension in their design, I'm not really qualified to tell them a cmpxchg syscall is a bad idea.

Quoted 4 lines

The problem is that by supporting these hypothetical designs that can't do atomics, you hurt sensible designs that *can* do the atomics because you force them to take an additional indirection that could otherwise be avoided.

I just went ahead and removed the system calls from the port -- they're not going to get called on any systems SiFive is building, so if someone complains then we'll just sort it out later.

author maintainer
#41 lore raw

Re: [PATCH 8/9] RISC-V: User-facing API

In reply to Christoph Hellwig
Quoted 6 lines · On Tue, 11 Jul 2017 06:55:28 PDT (-0700), [email protected] wrote:
Quoted 4 lines · Will Deacon

The problem is that by supporting these hypothetical designs that can't do atomics, you hurt sensible designs that *can* do the atomics because you force them to take an additional indirection that could otherwise be avoided.

Quoted 7 lines

Agreed. But the new patchset seems to remove it already, so I guess we're fine on the kernel side. Now we just need to make sure the glibc API doesn't use any indirections.

Note that it might make sense to emit these for very low end nommu designs. Maybe even running Linux, but in that case they'll just need a special non-standard ABI for very limited use cases.

glibc has never used these calls on machines with the A extension. They're only used in one specific header file to emulate cmpxchg, and they're guarded by something like "#ifdef riscv_atomic". Here's the glibc code (from a slightly older version, glibc is also in submission so everything's a bit of a mess there too) for reference

/* If the A (atomic) extension is not present, we need help from the
kernel to do atomic accesses. Linux provides two system calls for this purpose. RISCV_ATOMIC_CMPXCHG will perform an atomic compare and exchange operation for a 32-bit value. RISCV_ATOMIC_CMPXCHG64 will do the same for a 64-bit value. */

  #include <sys/syscall.h>
  #include <sysdep.h>

  #define __HAVE_64B_ATOMICS (__riscv_xlen >= 64)
  #define USE_ATOMIC_COMPILER_BUILTINS 0

  #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
    (abort (), (__typeof (*mem)) 0)

  #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
    (abort (), (__typeof (*mem)) 0)

  /* The only basic operation needed is compare and exchange.  */
  #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
    ({									      \
      INTERNAL_SYSCALL_DECL (__err);					      \
      (__typeof (*mem)) INTERNAL_SYSCALL (sysriscv, __err, 4,		      \
  		      RISCV_ATOMIC_CMPXCHG, mem, oldval, newval);	      \
    })

  #define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
    ({									      \
      INTERNAL_SYSCALL_DECL (__err);					      \
      (__typeof (*mem)) INTERNAL_SYSCALL (sysriscv, __err, 4,		      \
  		      RISCV_ATOMIC_CMPXCHG64, mem, oldval, newval);	      \
    })

We originally had these as a special Kconfig option, but then it was pointed out that user binaries built on non-A systems wouldn't run on A systems. That seemed like a bad idea, so we just enabled it everywhere.

I think we should just table this discussion for now: we can always add the system calls back in if people build non-A Linux systems. We'll mark our glibc port as requiring the A extension and delete the dead code there so nothing knows about the syscalls.

Thanks!