1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Regents of the University of California 4 * Copyright (C) 2014 Darius Rad <darius@bluespec.com> 5 * Copyright (C) 2017 SiFive 6 */ 7 8 #include <linux/syscalls.h> 9 #include <asm/cacheflush.h> 10 #include <asm/cpufeature.h> 11 #include <asm/hwprobe.h> 12 #include <asm/sbi.h> 13 #include <asm/vector.h> 14 #include <asm/switch_to.h> 15 #include <asm/uaccess.h> 16 #include <asm/unistd.h> 17 #include <asm-generic/mman-common.h> 18 #include <vdso/vsyscall.h> 19 20 static long riscv_sys_mmap(unsigned long addr, unsigned long len, 21 unsigned long prot, unsigned long flags, 22 unsigned long fd, off_t offset, 23 unsigned long page_shift_offset) 24 { 25 if (unlikely(offset & (~PAGE_MASK >> page_shift_offset))) 26 return -EINVAL; 27 28 return ksys_mmap_pgoff(addr, len, prot, flags, fd, 29 offset >> (PAGE_SHIFT - page_shift_offset)); 30 } 31 32 #ifdef CONFIG_64BIT 33 SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len, 34 unsigned long, prot, unsigned long, flags, 35 unsigned long, fd, off_t, offset) 36 { 37 return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 0); 38 } 39 #endif 40 41 #if defined(CONFIG_32BIT) || defined(CONFIG_COMPAT) 42 SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len, 43 unsigned long, prot, unsigned long, flags, 44 unsigned long, fd, off_t, offset) 45 { 46 /* 47 * Note that the shift for mmap2 is constant (12), 48 * regardless of PAGE_SIZE 49 */ 50 return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 12); 51 } 52 #endif 53 54 /* 55 * Allows the instruction cache to be flushed from userspace. Despite RISC-V 56 * having a direct 'fence.i' instruction available to userspace (which we 57 * can't trap!), that's not actually viable when running on Linux because the 58 * kernel might schedule a process on another hart. There is no way for 59 * userspace to handle this without invoking the kernel (as it doesn't know the 60 * thread->hart mappings), so we've defined a RISC-V specific system call to 61 * flush the instruction cache. 62 * 63 * sys_riscv_flush_icache() is defined to flush the instruction cache over an 64 * address range, with the flush applying to either all threads or just the 65 * caller. We don't currently do anything with the address range, that's just 66 * in there for forwards compatibility. 67 */ 68 SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end, 69 uintptr_t, flags) 70 { 71 /* Check the reserved flags. */ 72 if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_ALL)) 73 return -EINVAL; 74 75 flush_icache_mm(current->mm, flags & SYS_RISCV_FLUSH_ICACHE_LOCAL); 76 77 return 0; 78 } 79 80 /* 81 * The hwprobe interface, for allowing userspace to probe to see which features 82 * are supported by the hardware. See Documentation/riscv/hwprobe.rst for more 83 * details. 84 */ 85 static void hwprobe_arch_id(struct riscv_hwprobe *pair, 86 const struct cpumask *cpus) 87 { 88 u64 id = -1ULL; 89 bool first = true; 90 int cpu; 91 92 for_each_cpu(cpu, cpus) { 93 u64 cpu_id; 94 95 switch (pair->key) { 96 case RISCV_HWPROBE_KEY_MVENDORID: 97 cpu_id = riscv_cached_mvendorid(cpu); 98 break; 99 case RISCV_HWPROBE_KEY_MIMPID: 100 cpu_id = riscv_cached_mimpid(cpu); 101 break; 102 case RISCV_HWPROBE_KEY_MARCHID: 103 cpu_id = riscv_cached_marchid(cpu); 104 break; 105 } 106 107 if (first) { 108 id = cpu_id; 109 first = false; 110 } 111 112 /* 113 * If there's a mismatch for the given set, return -1 in the 114 * value. 115 */ 116 if (id != cpu_id) { 117 id = -1ULL; 118 break; 119 } 120 } 121 122 pair->value = id; 123 } 124 125 static u64 hwprobe_misaligned(const struct cpumask *cpus) 126 { 127 int cpu; 128 u64 perf = -1ULL; 129 130 for_each_cpu(cpu, cpus) { 131 int this_perf = per_cpu(misaligned_access_speed, cpu); 132 133 if (perf == -1ULL) 134 perf = this_perf; 135 136 if (perf != this_perf) { 137 perf = RISCV_HWPROBE_MISALIGNED_UNKNOWN; 138 break; 139 } 140 } 141 142 if (perf == -1ULL) 143 return RISCV_HWPROBE_MISALIGNED_UNKNOWN; 144 145 return perf; 146 } 147 148 static void hwprobe_one_pair(struct riscv_hwprobe *pair, 149 const struct cpumask *cpus) 150 { 151 switch (pair->key) { 152 case RISCV_HWPROBE_KEY_MVENDORID: 153 case RISCV_HWPROBE_KEY_MARCHID: 154 case RISCV_HWPROBE_KEY_MIMPID: 155 hwprobe_arch_id(pair, cpus); 156 break; 157 /* 158 * The kernel already assumes that the base single-letter ISA 159 * extensions are supported on all harts, and only supports the 160 * IMA base, so just cheat a bit here and tell that to 161 * userspace. 162 */ 163 case RISCV_HWPROBE_KEY_BASE_BEHAVIOR: 164 pair->value = RISCV_HWPROBE_BASE_BEHAVIOR_IMA; 165 break; 166 167 case RISCV_HWPROBE_KEY_IMA_EXT_0: 168 pair->value = 0; 169 if (has_fpu()) 170 pair->value |= RISCV_HWPROBE_IMA_FD; 171 172 if (riscv_isa_extension_available(NULL, c)) 173 pair->value |= RISCV_HWPROBE_IMA_C; 174 175 if (has_vector()) 176 pair->value |= RISCV_HWPROBE_IMA_V; 177 178 break; 179 180 case RISCV_HWPROBE_KEY_CPUPERF_0: 181 pair->value = hwprobe_misaligned(cpus); 182 break; 183 184 /* 185 * For forward compatibility, unknown keys don't fail the whole 186 * call, but get their element key set to -1 and value set to 0 187 * indicating they're unrecognized. 188 */ 189 default: 190 pair->key = -1; 191 pair->value = 0; 192 break; 193 } 194 } 195 196 static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs, 197 size_t pair_count, size_t cpu_count, 198 unsigned long __user *cpus_user, 199 unsigned int flags) 200 { 201 size_t out; 202 int ret; 203 cpumask_t cpus; 204 205 /* Check the reserved flags. */ 206 if (flags != 0) 207 return -EINVAL; 208 209 /* 210 * The interface supports taking in a CPU mask, and returns values that 211 * are consistent across that mask. Allow userspace to specify NULL and 212 * 0 as a shortcut to all online CPUs. 213 */ 214 cpumask_clear(&cpus); 215 if (!cpu_count && !cpus_user) { 216 cpumask_copy(&cpus, cpu_online_mask); 217 } else { 218 if (cpu_count > cpumask_size()) 219 cpu_count = cpumask_size(); 220 221 ret = copy_from_user(&cpus, cpus_user, cpu_count); 222 if (ret) 223 return -EFAULT; 224 225 /* 226 * Userspace must provide at least one online CPU, without that 227 * there's no way to define what is supported. 228 */ 229 cpumask_and(&cpus, &cpus, cpu_online_mask); 230 if (cpumask_empty(&cpus)) 231 return -EINVAL; 232 } 233 234 for (out = 0; out < pair_count; out++, pairs++) { 235 struct riscv_hwprobe pair; 236 237 if (get_user(pair.key, &pairs->key)) 238 return -EFAULT; 239 240 pair.value = 0; 241 hwprobe_one_pair(&pair, &cpus); 242 ret = put_user(pair.key, &pairs->key); 243 if (ret == 0) 244 ret = put_user(pair.value, &pairs->value); 245 246 if (ret) 247 return -EFAULT; 248 } 249 250 return 0; 251 } 252 253 #ifdef CONFIG_MMU 254 255 static int __init init_hwprobe_vdso_data(void) 256 { 257 struct vdso_data *vd = __arch_get_k_vdso_data(); 258 struct arch_vdso_data *avd = &vd->arch_data; 259 u64 id_bitsmash = 0; 260 struct riscv_hwprobe pair; 261 int key; 262 263 /* 264 * Initialize vDSO data with the answers for the "all CPUs" case, to 265 * save a syscall in the common case. 266 */ 267 for (key = 0; key <= RISCV_HWPROBE_MAX_KEY; key++) { 268 pair.key = key; 269 hwprobe_one_pair(&pair, cpu_online_mask); 270 271 WARN_ON_ONCE(pair.key < 0); 272 273 avd->all_cpu_hwprobe_values[key] = pair.value; 274 /* 275 * Smash together the vendor, arch, and impl IDs to see if 276 * they're all 0 or any negative. 277 */ 278 if (key <= RISCV_HWPROBE_KEY_MIMPID) 279 id_bitsmash |= pair.value; 280 } 281 282 /* 283 * If the arch, vendor, and implementation ID are all the same across 284 * all harts, then assume all CPUs are the same, and allow the vDSO to 285 * answer queries for arbitrary masks. However if all values are 0 (not 286 * populated) or any value returns -1 (varies across CPUs), then the 287 * vDSO should defer to the kernel for exotic cpu masks. 288 */ 289 avd->homogeneous_cpus = id_bitsmash != 0 && id_bitsmash != -1; 290 return 0; 291 } 292 293 arch_initcall_sync(init_hwprobe_vdso_data); 294 295 #endif /* CONFIG_MMU */ 296 297 SYSCALL_DEFINE5(riscv_hwprobe, struct riscv_hwprobe __user *, pairs, 298 size_t, pair_count, size_t, cpu_count, unsigned long __user *, 299 cpus, unsigned int, flags) 300 { 301 return do_riscv_hwprobe(pairs, pair_count, cpu_count, 302 cpus, flags); 303 } 304