source: mainline/kernel/generic/src/synch/syswaitq.c@ 6068476

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6068476 was 5a5269d, checked in by GitHub <noreply@…>, 6 years ago

Change type of uspace pointers in kernel from pointer type to numeric (#170)

From kernel's perspective, userspace addresses are not valid pointers,
and can only be used in calls to copy_to/from_uspace().
Therefore, we change the type of those arguments and variables to
uspace_addr_t which is an alias for sysarg_t.

This allows the compiler to catch accidental direct accesses to
userspace addresses.

Additionally, to avoid losing the type information in code,
a macro uspace_ptr(type) is used that translates to uspace_addr_t.
I makes no functional difference, but allows keeping the type information
in code in case we implement some sort of static checking for it in the future.

However, ccheck doesn't like that, so instead of using uspace_ptr(char),
we use uspace_ptr_char which is defined as
#define uspace_ptr_char uspace_ptr(char).

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2018 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup kernel_sync
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Wrapper for using wait queue as a kobject.
36 */
37
38#include <synch/syswaitq.h>
39#include <synch/waitq.h>
40#include <abi/cap.h>
41#include <cap/cap.h>
42#include <mm/slab.h>
43#include <proc/task.h>
44#include <syscall/copy.h>
45
46#include <stdint.h>
47
48static slab_cache_t *waitq_cache;
49
50static void waitq_destroy(void *arg)
51{
52 waitq_t *wq = (waitq_t *) arg;
53 slab_free(waitq_cache, wq);
54}
55
56static kobject_ops_t waitq_kobject_ops = {
57 .destroy = waitq_destroy
58};
59
60static bool waitq_cap_cleanup_cb(cap_t *cap, void *arg)
61{
62 kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
63 KOBJECT_TYPE_WAITQ);
64 kobject_put(kobj);
65 cap_free(cap->task, cap->handle);
66 return true;
67}
68
69/** Initialize the user waitq subsystem */
70void sys_waitq_init(void)
71{
72 waitq_cache = slab_cache_create("waitq_t", sizeof(waitq_t), 0, NULL,
73 NULL, 0);
74}
75
76/** Clean-up all waitq capabilities held by the exiting task */
77void sys_waitq_task_cleanup(void)
78{
79 caps_apply_to_kobject_type(TASK, KOBJECT_TYPE_WAITQ,
80 waitq_cap_cleanup_cb, NULL);
81}
82
83/** Create a waitq for the current task
84 *
85 * @param[out] whandle Userspace address of the destination buffer that will
86 * receive the allocated waitq capability.
87 *
88 * @return Error code.
89 */
90sys_errno_t sys_waitq_create(uspace_ptr_cap_waitq_handle_t whandle)
91{
92 waitq_t *wq = slab_alloc(waitq_cache, FRAME_ATOMIC);
93 if (!wq)
94 return (sys_errno_t) ENOMEM;
95 waitq_initialize(wq);
96
97 kobject_t *kobj = kobject_alloc(0);
98 if (!kobj) {
99 slab_free(waitq_cache, wq);
100 return (sys_errno_t) ENOMEM;
101 }
102 kobject_initialize(kobj, KOBJECT_TYPE_WAITQ, wq, &waitq_kobject_ops);
103
104 cap_handle_t handle;
105 errno_t rc = cap_alloc(TASK, &handle);
106 if (rc != EOK) {
107 slab_free(waitq_cache, wq);
108 kobject_free(kobj);
109 return (sys_errno_t) rc;
110 }
111
112 rc = copy_to_uspace(whandle, &handle, sizeof(handle));
113 if (rc != EOK) {
114 cap_free(TASK, handle);
115 kobject_free(kobj);
116 slab_free(waitq_cache, wq);
117 return (sys_errno_t) rc;
118 }
119
120 cap_publish(TASK, handle, kobj);
121
122 return (sys_errno_t) EOK;
123}
124
125/** Destroy a waitq
126 *
127 * @param whandle Waitq capability handle of the waitq to be destroyed.
128 *
129 * @return Error code.
130 */
131sys_errno_t sys_waitq_destroy(cap_waitq_handle_t whandle)
132{
133 kobject_t *kobj = cap_unpublish(TASK, whandle, KOBJECT_TYPE_WAITQ);
134 if (!kobj)
135 return (sys_errno_t) ENOENT;
136 kobject_put(kobj);
137 cap_free(TASK, whandle);
138 return EOK;
139}
140
141/** Sleep in the waitq
142 *
143 * @param whandle Waitq capability handle of the waitq in which to sleep.
144 * @param timeout Timeout in microseconds.
145 * @param flags Flags from SYNCH_FLAGS_* family. SYNCH_FLAGS_INTERRUPTIBLE is
146 * always implied.
147 *
148 * @return Error code.
149 */
150sys_errno_t sys_waitq_sleep(cap_waitq_handle_t whandle, uint32_t timeout,
151 unsigned int flags)
152{
153 kobject_t *kobj = kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
154 if (!kobj)
155 return (sys_errno_t) ENOENT;
156
157#ifdef CONFIG_UDEBUG
158 udebug_stoppable_begin();
159#endif
160
161 errno_t rc = waitq_sleep_timeout(kobj->waitq, timeout,
162 SYNCH_FLAGS_INTERRUPTIBLE | flags, NULL);
163
164#ifdef CONFIG_UDEBUG
165 udebug_stoppable_end();
166#endif
167
168 kobject_put(kobj);
169
170 return (sys_errno_t) rc;
171}
172
173/** Wakeup a thread sleeping in the waitq
174 *
175 * @param whandle Waitq capability handle of the waitq to invoke wakeup on.
176 *
177 * @return Error code.
178 */
179sys_errno_t sys_waitq_wakeup(cap_waitq_handle_t whandle)
180{
181 kobject_t *kobj = kobject_get(TASK, whandle, KOBJECT_TYPE_WAITQ);
182 if (!kobj)
183 return (sys_errno_t) ENOENT;
184
185 waitq_wakeup(kobj->waitq, WAKEUP_FIRST);
186
187 kobject_put(kobj);
188 return (sys_errno_t) EOK;
189}
190
191/** @}
192 */
Note: See TracBrowser for help on using the repository browser.