source: mainline/kernel/generic/src/cap/cap.c@ 94868e1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 94868e1 was 6636fb19, checked in by Jakub Jermar <jakub@…>, 8 years ago

Add comments

  • Property mode set to 100644
File size: 11.2 KB
Line 
1/*
2 * Copyright (c) 2017 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 generic
30 * @{
31 */
32/** @file
33 */
34
35/*
36 * HelenOS capabilities are task-local names for references to kernel objects.
37 * Kernel objects are reference-counted wrappers for a select group of objects
38 * allocated in and by the kernel that can be made accessible to userspace in a
39 * controlled way via integer handles.
40 *
41 * A kernel object (kobject_t) encapsulates one of the following raw objects:
42 *
43 * - IPC phone
44 * - IRQ object
45 *
46 * A capability (cap_t) is either free, allocated or published. Free
47 * capabilities can be allocated, which reserves the capability handle in the
48 * task-local capability space. Allocated capabilities can be published, which
49 * associates them with an existing kernel object. Userspace can only access
50 * published capabilities.
51 *
52 * A published capability may get unpublished, which disassociates it from the
53 * underlying kernel object and puts it back into the allocated state. An
54 * allocated capability can be freed to become available for future use.
55 *
56 * There is a 1:1 correspondence between a kernel object (kobject_t) and the
57 * actual raw object it encapsulates. A kernel object (kobject_t) may have
58 * multiple references, either implicit from one or more capabilities (cap_t),
59 * even from capabilities in different tasks, or explicit as a result of
60 * creating a new reference from a capability handle using kobject_get(), or
61 * creating a new reference from an already existing reference by
62 * kobject_add_ref() or as a result of unpublishing a capability and
63 * disassociating it from its kobject_t using cap_unpublish().
64 *
65 * As kernel objects are reference-counted, they get automatically destroyed
66 * when their last reference is dropped in kobject_put(). The idea is that
67 * whenever a kernel object is inserted into some sort of a container (e.g. a
68 * list or hash table), its reference count should be incremented via
69 * kobject_get() or kobject_add_ref(). When the kernel object is removed from
70 * the container, the reference count should go down via a call to
71 * kobject_put().
72 */
73
74#include <cap/cap.h>
75#include <proc/task.h>
76#include <synch/mutex.h>
77#include <abi/errno.h>
78#include <mm/slab.h>
79#include <adt/list.h>
80
81static kobject_t *cap_unpublish_locked(task_t *, cap_handle_t, kobject_type_t);
82
83/** Initialize capability and associate it with its handle
84 *
85 * @param cap Address of the capability.
86 * @param handle Capability handle.
87 */
88void cap_initialize(cap_t *cap, cap_handle_t handle)
89{
90 cap->state = CAP_STATE_FREE;
91 cap->handle = handle;
92 link_initialize(&cap->link);
93}
94
95/** Allocate the capability info structure
96 *
97 * @param task Task for which to allocate the info structure.
98 */
99void caps_task_alloc(task_t *task)
100{
101 task->cap_info = (cap_info_t *) malloc(sizeof(cap_info_t), 0);
102 task->cap_info->caps = malloc(sizeof(cap_t) * MAX_CAPS, 0);
103}
104
105/** Initialize the capability info structure
106 *
107 * @param task Task for which to initialize the info structure.
108 */
109void caps_task_init(task_t *task)
110{
111 mutex_initialize(&task->cap_info->lock, MUTEX_PASSIVE);
112
113 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
114 list_initialize(&task->cap_info->type_list[t]);
115
116 for (cap_handle_t h = 0; h < MAX_CAPS; h++)
117 cap_initialize(&task->cap_info->caps[h], h);
118}
119
120/** Deallocate the capability info structure
121 *
122 * @param task Task from which to deallocate the info structure.
123 */
124void caps_task_free(task_t *task)
125{
126 free(task->cap_info->caps);
127 free(task->cap_info);
128}
129
130/** Invoke callback function on task's capabilites of given type
131 *
132 * @param task Task where the invocation should take place.
133 * @param type Kernel object type of the task's capabilities that will be
134 * subject to the callback invocation.
135 * @param cb Callback function.
136 * @param arg Argument for the callback function.
137 *
138 * @return True if the callback was called on all matching capabilities.
139 * @return False if the callback was applied only partially.
140 */
141bool caps_apply_to_kobject_type(task_t *task, kobject_type_t type,
142 bool (*cb)(cap_t *, void *), void *arg)
143{
144 bool done = true;
145
146 mutex_lock(&task->cap_info->lock);
147 list_foreach_safe(task->cap_info->type_list[type], cur, next) {
148 cap_t *cap = list_get_instance(cur, cap_t, link);
149 done = cb(cap, arg);
150 if (!done)
151 break;
152 }
153 mutex_unlock(&task->cap_info->lock);
154
155 return done;
156}
157
158/** Get capability using capability handle
159 *
160 * @param task Task whose capability to get.
161 * @param handle Capability handle of the desired capability.
162 * @param state State in which the capability must be.
163 *
164 * @return Address of the desired capability if it exists and its state matches.
165 * @return NULL if no such capability exists or it's in a different state.
166 */
167static cap_t *cap_get(task_t *task, cap_handle_t handle, cap_state_t state)
168{
169 assert(mutex_locked(&task->cap_info->lock));
170
171 if ((handle < 0) || (handle >= MAX_CAPS))
172 return NULL;
173 if (task->cap_info->caps[handle].state != state)
174 return NULL;
175 return &task->cap_info->caps[handle];
176}
177
178/** Allocate new capability
179 *
180 * @param task Task for which to allocate the new capability.
181 *
182 * @return New capability handle on success.
183 * @return Negative error code in case of error.
184 */
185cap_handle_t cap_alloc(task_t *task)
186{
187 mutex_lock(&task->cap_info->lock);
188 for (cap_handle_t handle = 0; handle < MAX_CAPS; handle++) {
189 cap_t *cap = &task->cap_info->caps[handle];
190 /* See if the capability should be garbage-collected */
191 if (cap->state == CAP_STATE_PUBLISHED &&
192 cap->kobject->ops->reclaim &&
193 cap->kobject->ops->reclaim(cap->kobject)) {
194 kobject_t *kobj = cap_unpublish_locked(task, handle,
195 cap->kobject->type);
196 kobject_put(kobj);
197 cap_initialize(&task->cap_info->caps[handle], handle);
198 }
199 if (cap->state == CAP_STATE_FREE) {
200 cap->state = CAP_STATE_ALLOCATED;
201 mutex_unlock(&task->cap_info->lock);
202 return handle;
203 }
204 }
205 mutex_unlock(&task->cap_info->lock);
206
207 return ELIMIT;
208}
209
210/** Publish allocated capability
211 *
212 * The kernel object is moved into the capability. In other words, its reference
213 * is handed over to the capability. Once published, userspace can access and
214 * manipulate the capability.
215 *
216 * @param task Task in which to publish the capability.
217 * @param handle Capability handle.
218 * @param kobj Kernel object.
219 */
220void
221cap_publish(task_t *task, cap_handle_t handle, kobject_t *kobj)
222{
223 mutex_lock(&task->cap_info->lock);
224 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
225 assert(cap);
226 cap->state = CAP_STATE_PUBLISHED;
227 /* Hand over kobj's reference to cap */
228 cap->kobject = kobj;
229 list_append(&cap->link, &task->cap_info->type_list[kobj->type]);
230 mutex_unlock(&task->cap_info->lock);
231}
232
233static kobject_t *
234cap_unpublish_locked(task_t *task, cap_handle_t handle, kobject_type_t type)
235{
236 kobject_t *kobj = NULL;
237
238 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
239 if (cap) {
240 if (cap->kobject->type == type) {
241 /* Hand over cap's reference to kobj */
242 kobj = cap->kobject;
243 cap->kobject = NULL;
244 list_remove(&cap->link);
245 cap->state = CAP_STATE_ALLOCATED;
246 }
247 }
248
249 return kobj;
250}
251
252/** Unpublish published capability
253 *
254 * The kernel object is moved out of the capability. In other words, the
255 * capability's reference to the objects is handed over to the kernel object
256 * pointer returned by this function. Once unpublished, the capability does not
257 * refer to any kernel object anymore.
258 *
259 * @param task Task in which to unpublish the capability.
260 * @param handle Capability handle.
261 * @param type Kernel object type of the object associated with the
262 * capability.
263 */
264kobject_t *cap_unpublish(task_t *task, cap_handle_t handle, kobject_type_t type)
265{
266
267 mutex_lock(&task->cap_info->lock);
268 kobject_t *kobj = cap_unpublish_locked(task, handle, type);
269 mutex_unlock(&task->cap_info->lock);
270
271 return kobj;
272}
273
274/** Free allocated capability
275 *
276 * @param task Task in which to free the capability.
277 * @param handle Capability handle.
278 */
279void cap_free(task_t *task, cap_handle_t handle)
280{
281 assert(handle >= 0);
282 assert(handle < MAX_CAPS);
283 assert(task->cap_info->caps[handle].state == CAP_STATE_ALLOCATED);
284
285 mutex_lock(&task->cap_info->lock);
286 cap_initialize(&task->cap_info->caps[handle], handle);
287 mutex_unlock(&task->cap_info->lock);
288}
289
290/** Initialize kernel object
291 *
292 * @param kobj Kernel object to initialize.
293 * @param type Type of the kernel object.
294 * @param raw Raw pointer to the encapsulated object.
295 * @param ops Pointer to kernel object operations for the respective type.
296 */
297void kobject_initialize(kobject_t *kobj, kobject_type_t type, void *raw,
298 kobject_ops_t *ops)
299{
300 atomic_set(&kobj->refcnt, 1);
301 kobj->type = type;
302 kobj->raw = raw;
303 kobj->ops = ops;
304}
305
306/** Get new reference to kernel object from capability
307 *
308 * @param task Task from which to get the reference.
309 * @param handle Capability handle.
310 * @param type Kernel object type of the object associated with the
311 * capability referenced by handle.
312 *
313 * @return Kernel object with incremented reference count on success.
314 * @return NULL if there is no matching capability or kernel object.
315 */
316kobject_t *
317kobject_get(struct task *task, cap_handle_t handle, kobject_type_t type)
318{
319 kobject_t *kobj = NULL;
320
321 mutex_lock(&task->cap_info->lock);
322 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
323 if (cap) {
324 if (cap->kobject->type == type) {
325 kobj = cap->kobject;
326 atomic_inc(&kobj->refcnt);
327 }
328 }
329 mutex_unlock(&task->cap_info->lock);
330
331 return kobj;
332}
333
334/** Record new reference
335 *
336 * @param kobj Kernel object from which the new reference is created.
337 */
338void kobject_add_ref(kobject_t *kobj)
339{
340 atomic_inc(&kobj->refcnt);
341}
342
343/** Drop reference to kernel object
344 *
345 * The encapsulated object and the kobject_t wrapper are both destroyed when the
346 * last reference is dropped.
347 *
348 * @param kobj Kernel object whose reference to drop.
349 */
350void kobject_put(kobject_t *kobj)
351{
352 if (atomic_postdec(&kobj->refcnt) == 1) {
353 kobj->ops->destroy(kobj->raw);
354 free(kobj);
355 }
356}
357
358/** @}
359 */
Note: See TracBrowser for help on using the repository browser.