xref: /openbmc/qemu/bsd-user/freebsd/os-misc.h (revision 80dcaf682a4399578edb4922472e599f014484e0)
1137d963cSStacey Son /*
2137d963cSStacey Son  *  miscellaneous FreeBSD system call shims
3137d963cSStacey Son  *
4137d963cSStacey Son  *  Copyright (c) 2013-14 Stacey D. Son
5137d963cSStacey Son  *
6137d963cSStacey Son  *  This program is free software; you can redistribute it and/or modify
7137d963cSStacey Son  *  it under the terms of the GNU General Public License as published by
8137d963cSStacey Son  *  the Free Software Foundation; either version 2 of the License, or
9137d963cSStacey Son  *  (at your option) any later version.
10137d963cSStacey Son  *
11137d963cSStacey Son  *  This program is distributed in the hope that it will be useful,
12137d963cSStacey Son  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13137d963cSStacey Son  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14137d963cSStacey Son  *  GNU General Public License for more details.
15137d963cSStacey Son  *
16137d963cSStacey Son  *  You should have received a copy of the GNU General Public License
17137d963cSStacey Son  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18137d963cSStacey Son  */
19137d963cSStacey Son 
20137d963cSStacey Son #ifndef OS_MISC_H
21137d963cSStacey Son #define OS_MISC_H
22137d963cSStacey Son 
23137d963cSStacey Son #include <sys/cpuset.h>
24137d963cSStacey Son #include <sys/random.h>
25137d963cSStacey Son #include <sched.h>
26137d963cSStacey Son 
270c352988SKarim Taha /*
280c352988SKarim Taha  * shm_open2 isn't exported, but the __sys_ alias is. We can use either for the
290c352988SKarim Taha  * static version, but to dynamically link we have to use the sys version.
300c352988SKarim Taha  */
310c352988SKarim Taha int __sys_shm_open2(const char *path, int flags, mode_t mode, int shmflags,
320c352988SKarim Taha     const char *);
330c352988SKarim Taha 
340c352988SKarim Taha #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
350c352988SKarim Taha /* shm_open2(2) */
do_freebsd_shm_open2(abi_ulong pathptr,abi_ulong flags,abi_long mode,abi_ulong shmflags,abi_ulong nameptr)360c352988SKarim Taha static inline abi_long do_freebsd_shm_open2(abi_ulong pathptr, abi_ulong flags,
370c352988SKarim Taha     abi_long mode, abi_ulong shmflags, abi_ulong nameptr)
380c352988SKarim Taha {
390c352988SKarim Taha     int ret;
400c352988SKarim Taha     void *uname, *upath;
410c352988SKarim Taha 
420c352988SKarim Taha     if (pathptr == (uintptr_t)SHM_ANON) {
430c352988SKarim Taha         upath = SHM_ANON;
440c352988SKarim Taha     } else {
450c352988SKarim Taha         upath = lock_user_string(pathptr);
460c352988SKarim Taha         if (upath == NULL) {
470c352988SKarim Taha             return -TARGET_EFAULT;
480c352988SKarim Taha         }
490c352988SKarim Taha     }
500c352988SKarim Taha 
510c352988SKarim Taha     uname = NULL;
520c352988SKarim Taha     if (nameptr != 0) {
530c352988SKarim Taha         uname = lock_user_string(nameptr);
540c352988SKarim Taha         if (uname == NULL) {
550c352988SKarim Taha             unlock_user(upath, pathptr, 0);
560c352988SKarim Taha             return -TARGET_EFAULT;
570c352988SKarim Taha         }
580c352988SKarim Taha     }
590c352988SKarim Taha     ret = get_errno(__sys_shm_open2(upath,
600c352988SKarim Taha                 target_to_host_bitmask(flags, fcntl_flags_tbl), mode,
610c352988SKarim Taha                 target_to_host_bitmask(shmflags, shmflag_flags_tbl), uname));
620c352988SKarim Taha 
630c352988SKarim Taha     if (upath != SHM_ANON) {
640c352988SKarim Taha         unlock_user(upath, pathptr, 0);
650c352988SKarim Taha     }
660c352988SKarim Taha     if (uname != NULL) {
670c352988SKarim Taha         unlock_user(uname, nameptr, 0);
680c352988SKarim Taha     }
690c352988SKarim Taha     return ret;
700c352988SKarim Taha }
710c352988SKarim Taha #endif /* __FreeBSD_version >= 1300048 */
720c352988SKarim Taha 
73*182ea728SKyle Evans #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300049
74*182ea728SKyle Evans /* shm_rename(2) */
do_freebsd_shm_rename(abi_ulong fromptr,abi_ulong toptr,abi_ulong flags)75*182ea728SKyle Evans static inline abi_long do_freebsd_shm_rename(abi_ulong fromptr, abi_ulong toptr,
76*182ea728SKyle Evans         abi_ulong flags)
77*182ea728SKyle Evans {
78*182ea728SKyle Evans     int ret;
79*182ea728SKyle Evans     void *ufrom, *uto;
80*182ea728SKyle Evans 
81*182ea728SKyle Evans     ufrom = lock_user_string(fromptr);
82*182ea728SKyle Evans     if (ufrom == NULL) {
83*182ea728SKyle Evans         return -TARGET_EFAULT;
84*182ea728SKyle Evans     }
85*182ea728SKyle Evans     uto = lock_user_string(toptr);
86*182ea728SKyle Evans     if (uto == NULL) {
87*182ea728SKyle Evans         unlock_user(ufrom, fromptr, 0);
88*182ea728SKyle Evans         return -TARGET_EFAULT;
89*182ea728SKyle Evans     }
90*182ea728SKyle Evans     ret = get_errno(shm_rename(ufrom, uto, flags));
91*182ea728SKyle Evans     unlock_user(ufrom, fromptr, 0);
92*182ea728SKyle Evans     unlock_user(uto, toptr, 0);
93*182ea728SKyle Evans 
94*182ea728SKyle Evans     return ret;
95*182ea728SKyle Evans }
96*182ea728SKyle Evans #endif /* __FreeBSD_version >= 1300049 */
97137d963cSStacey Son 
98137d963cSStacey Son #endif /* OS_MISC_H */
99