hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (C) 2001,2002,2003,2004,2005 Philippe Gerum <rpm@xenomai.org>.
 *
 * Xenomai 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.
 *
 * Xenomai 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 Xenomai; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */
#ifndef _COBALT_ASM_GENERIC_SYSCALL_H
#define _COBALT_ASM_GENERIC_SYSCALL_H
 
#include <linux/types.h>
#include <linux/version.h>
#include <linux/uaccess.h>
#include <asm/xenomai/features.h>
#include <asm/xenomai/wrappers.h>
#include <asm/xenomai/machine.h>
#include <cobalt/uapi/asm-generic/syscall.h>
#include <cobalt/uapi/kernel/types.h>
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
#define access_rok(addr, size)    access_ok((addr), (size))
#define access_wok(addr, size)    access_ok((addr), (size))
#else
#define access_rok(addr, size)    access_ok(VERIFY_READ, (addr), (size))
#define access_wok(addr, size)    access_ok(VERIFY_WRITE, (addr), (size))
#endif
 
#define __xn_copy_from_user(dstP, srcP, n)    raw_copy_from_user(dstP, srcP, n)
#define __xn_copy_to_user(dstP, srcP, n)    raw_copy_to_user(dstP, srcP, n)
#define __xn_put_user(src, dstP)        __put_user(src, dstP)
#define __xn_get_user(dst, srcP)        __get_user(dst, srcP)
#define __xn_strncpy_from_user(dstP, srcP, n)    strncpy_from_user(dstP, srcP, n)
 
static inline int cobalt_copy_from_user(void *dst, const void __user *src,
                   size_t size)
{
   size_t remaining = size;
 
   if (likely(access_rok(src, size)))
       remaining = __xn_copy_from_user(dst, src, size);
 
   if (unlikely(remaining > 0)) {
       memset(dst + (size - remaining), 0, remaining);
       return -EFAULT;
   }
   return 0;
}
 
static inline int cobalt_copy_to_user(void __user *dst, const void *src,
                     size_t size)
{
   if (unlikely(!access_wok(dst, size) ||
       __xn_copy_to_user(dst, src, size)))
       return -EFAULT;
   return 0;
}
 
static inline int cobalt_strncpy_from_user(char *dst, const char __user *src,
                      size_t count)
{
   if (unlikely(!access_rok(src, 1)))
       return -EFAULT;
 
   return __xn_strncpy_from_user(dst, src, count);
}
 
 
/*
 * NOTE: those copy helpers won't work in compat mode: use
 * sys32_get_*(), sys32_put_*() instead.
 */
 
static inline int cobalt_get_u_timespec(struct timespec64 *dst,
           const struct __user_old_timespec __user *src)
{
   struct __user_old_timespec u_ts;
   int ret;
 
   ret = cobalt_copy_from_user(&u_ts, src, sizeof(u_ts));
   if (ret)
       return ret;
 
   dst->tv_sec = u_ts.tv_sec;
   dst->tv_nsec = u_ts.tv_nsec;
 
   return 0;
}
 
static inline int cobalt_put_u_timespec(
   struct __user_old_timespec __user *dst,
   const struct timespec64 *src)
{
   struct __user_old_timespec u_ts;
   int ret;
 
   u_ts.tv_sec = src->tv_sec;
   u_ts.tv_nsec = src->tv_nsec;
 
   ret = cobalt_copy_to_user(dst, &u_ts, sizeof(*dst));
   if (ret)
       return ret;
 
   return 0;
}
 
static inline int cobalt_get_u_itimerspec(struct itimerspec64 *dst,
           const struct __user_old_itimerspec __user *src)
{
   struct __user_old_itimerspec u_its;
   int ret;
 
   ret = cobalt_copy_from_user(&u_its, src, sizeof(u_its));
   if (ret)
       return ret;
 
   dst->it_interval.tv_sec = u_its.it_interval.tv_sec;
   dst->it_interval.tv_nsec = u_its.it_interval.tv_nsec;
   dst->it_value.tv_sec = u_its.it_value.tv_sec;
   dst->it_value.tv_nsec = u_its.it_value.tv_nsec;
 
   return 0;
}
 
static inline int cobalt_put_u_itimerspec(
   struct __user_old_itimerspec __user *dst,
   const struct itimerspec64 *src)
{
   struct __user_old_itimerspec u_its;
 
   u_its.it_interval.tv_sec = src->it_interval.tv_sec;
   u_its.it_interval.tv_nsec = src->it_interval.tv_nsec;
   u_its.it_value.tv_sec = src->it_value.tv_sec;
   u_its.it_value.tv_nsec = src->it_value.tv_nsec;
 
   return cobalt_copy_to_user(dst, &u_its, sizeof(*dst));
}
 
/* 32bit syscall emulation */
#define __COBALT_COMPAT_BIT    0x1
/* 32bit syscall emulation - extended form */
#define __COBALT_COMPATX_BIT    0x2
 
#endif /* !_COBALT_ASM_GENERIC_SYSCALL_H */