1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __TRACE_PROBE_KERNEL_H_
3 #define __TRACE_PROBE_KERNEL_H_
6 * This depends on trace_probe.h, but can not include it due to
7 * the way trace_probe_tmpl.h is used by trace_kprobe.c and trace_eprobe.c.
8 * Which means that any other user must include trace_probe.h before including
11 /* Return the length of string -- including null terminal byte */
12 static nokprobe_inline int
13 fetch_store_strlen_user(unsigned long addr)
15 const void __user *uaddr = (__force const void __user *)addr;
17 return strnlen_user_nofault(uaddr, MAX_STRING_SIZE);
20 /* Return the length of string -- including null terminal byte */
21 static nokprobe_inline int
22 fetch_store_strlen(unsigned long addr)
27 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
29 return fetch_store_strlen_user(addr);
33 ret = copy_from_kernel_nofault(&c, (u8 *)addr + len, 1);
35 } while (c && ret == 0 && len < MAX_STRING_SIZE);
37 return (ret < 0) ? ret : len;
40 static nokprobe_inline void set_data_loc(int ret, void *dest, void *__dest, void *base)
44 *(u32 *)dest = make_data_loc(ret, __dest - base);
48 * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
49 * with max length and relative data location.
51 static nokprobe_inline int
52 fetch_store_string_user(unsigned long addr, void *dest, void *base)
54 const void __user *uaddr = (__force const void __user *)addr;
55 int maxlen = get_loc_len(*(u32 *)dest);
59 if (unlikely(!maxlen))
62 __dest = get_loc_data(dest, base);
64 ret = strncpy_from_user_nofault(__dest, uaddr, maxlen);
65 set_data_loc(ret, dest, __dest, base);
71 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
72 * length and relative data location.
74 static nokprobe_inline int
75 fetch_store_string(unsigned long addr, void *dest, void *base)
77 int maxlen = get_loc_len(*(u32 *)dest);
81 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
82 if ((unsigned long)addr < TASK_SIZE)
83 return fetch_store_string_user(addr, dest, base);
86 if (unlikely(!maxlen))
89 __dest = get_loc_data(dest, base);
92 * Try to get string again, since the string can be changed while
95 ret = strncpy_from_kernel_nofault(__dest, (void *)addr, maxlen);
96 set_data_loc(ret, dest, __dest, base);
101 static nokprobe_inline int
102 probe_mem_read_user(void *dest, void *src, size_t size)
104 const void __user *uaddr = (__force const void __user *)src;
106 return copy_from_user_nofault(dest, uaddr, size);
109 static nokprobe_inline int
110 probe_mem_read(void *dest, void *src, size_t size)
112 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
113 if ((unsigned long)src < TASK_SIZE)
114 return probe_mem_read_user(dest, src, size);
116 return copy_from_kernel_nofault(dest, src, size);
119 #endif /* __TRACE_PROBE_KERNEL_H_ */