source: mainline/kernel/generic/src/cap/cap.c@ f571ca49

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

Add CAP_NIL

Sometimes it is useful to have a capability analogy of NULL.

  • Property mode set to 100644
File size: 13.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 <abi/cap.h>
76#include <proc/task.h>
77#include <synch/mutex.h>
78#include <abi/errno.h>
79#include <mm/slab.h>
80#include <adt/list.h>
81
82#include <stdint.h>
83
84#define CAPS_START (CAP_NIL + 1)
85#define CAPS_SIZE (INT_MAX - CAPS_START)
86#define CAPS_LAST (CAPS_SIZE - 1)
87
88static slab_cache_t *cap_slab;
89
90static size_t caps_hash(const ht_link_t *item)
91{
92 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
93 return hash_mix(cap->handle);
94}
95
96static size_t caps_key_hash(void *key)
97{
98 cap_handle_t *handle = (cap_handle_t *) key;
99 return hash_mix(*handle);
100}
101
102static bool caps_key_equal(void *key, const ht_link_t *item)
103{
104 cap_handle_t *handle = (cap_handle_t *) key;
105 cap_t *cap = hash_table_get_inst(item, cap_t, caps_link);
106 return *handle == cap->handle;
107}
108
109static hash_table_ops_t caps_ops = {
110 .hash = caps_hash,
111 .key_hash = caps_key_hash,
112 .key_equal = caps_key_equal
113};
114
115void caps_init(void)
116{
117 cap_slab = slab_cache_create("cap_t", sizeof(cap_t), 0, NULL,
118 NULL, 0);
119}
120
121/** Allocate the capability info structure
122 *
123 * @param task Task for which to allocate the info structure.
124 */
125int caps_task_alloc(task_t *task)
126{
127 task->cap_info = (cap_info_t *) malloc(sizeof(cap_info_t),
128 FRAME_ATOMIC);
129 if (!task->cap_info)
130 return ENOMEM;
131 task->cap_info->handles = ra_arena_create();
132 if (!task->cap_info->handles)
133 goto error_handles;
134 if (!ra_span_add(task->cap_info->handles, CAPS_START, CAPS_SIZE))
135 goto error_span;
136 if (!hash_table_create(&task->cap_info->caps, 0, 0, &caps_ops))
137 goto error_span;
138 return EOK;
139
140error_span:
141 ra_arena_destroy(task->cap_info->handles);
142error_handles:
143 free(task->cap_info);
144 return ENOMEM;
145}
146
147/** Initialize the capability info structure
148 *
149 * @param task Task for which to initialize the info structure.
150 */
151void caps_task_init(task_t *task)
152{
153 mutex_initialize(&task->cap_info->lock, MUTEX_RECURSIVE);
154
155 for (kobject_type_t t = 0; t < KOBJECT_TYPE_MAX; t++)
156 list_initialize(&task->cap_info->type_list[t]);
157}
158
159/** Deallocate the capability info structure
160 *
161 * @param task Task from which to deallocate the info structure.
162 */
163void caps_task_free(task_t *task)
164{
165 hash_table_destroy(&task->cap_info->caps);
166 ra_arena_destroy(task->cap_info->handles);
167 free(task->cap_info);
168}
169
170/** Invoke callback function on task's capabilites of given type
171 *
172 * @param task Task where the invocation should take place.
173 * @param type Kernel object type of the task's capabilities that will be
174 * subject to the callback invocation.
175 * @param cb Callback function.
176 * @param arg Argument for the callback function.
177 *
178 * @return True if the callback was called on all matching capabilities.
179 * @return False if the callback was applied only partially.
180 */
181bool caps_apply_to_kobject_type(task_t *task, kobject_type_t type,
182 bool (*cb)(cap_t *, void *), void *arg)
183{
184 bool done = true;
185
186 mutex_lock(&task->cap_info->lock);
187 list_foreach_safe(task->cap_info->type_list[type], cur, next) {
188 cap_t *cap = list_get_instance(cur, cap_t, type_link);
189 done = cb(cap, arg);
190 if (!done)
191 break;
192 }
193 mutex_unlock(&task->cap_info->lock);
194
195 return done;
196}
197
198/** Initialize capability and associate it with its handle
199 *
200 * @param cap Address of the capability.
201 * @param task Backling to the owning task.
202 * @param handle Capability handle.
203 */
204static void cap_initialize(cap_t *cap, task_t *task, cap_handle_t handle)
205{
206 cap->state = CAP_STATE_FREE;
207 cap->task = task;
208 cap->handle = handle;
209 link_initialize(&cap->type_link);
210}
211
212/** Get capability using capability handle
213 *
214 * @param task Task whose capability to get.
215 * @param handle Capability handle of the desired capability.
216 * @param state State in which the capability must be.
217 *
218 * @return Address of the desired capability if it exists and its state matches.
219 * @return NULL if no such capability exists or it's in a different state.
220 */
221static cap_t *cap_get(task_t *task, cap_handle_t handle, cap_state_t state)
222{
223 assert(mutex_locked(&task->cap_info->lock));
224
225 if ((handle < CAPS_START) || (handle > CAPS_LAST))
226 return NULL;
227 ht_link_t *link = hash_table_find(&task->cap_info->caps, &handle);
228 if (!link)
229 return NULL;
230 cap_t *cap = hash_table_get_inst(link, cap_t, caps_link);
231 if (cap->state != state)
232 return NULL;
233 return cap;
234}
235
236static bool cap_reclaimer(ht_link_t *link, void *arg)
237{
238 cap_t **result = (cap_t **) arg;
239 cap_t *cap = hash_table_get_inst(link, cap_t, caps_link);
240
241 if (cap->state == CAP_STATE_PUBLISHED && cap->kobject->ops->reclaim &&
242 cap->kobject->ops->reclaim(cap->kobject)) {
243 kobject_t *kobj = cap_unpublish(cap->task, cap->handle,
244 cap->kobject->type);
245 kobject_put(kobj);
246 cap_initialize(cap, cap->task, cap->handle);
247 *result = cap;
248 return false;
249 }
250
251 return true;
252}
253
254/** Allocate new capability
255 *
256 * @param task Task for which to allocate the new capability.
257 *
258 * @return New capability handle on success.
259 * @return Negative error code in case of error.
260 */
261cap_handle_t cap_alloc(task_t *task)
262{
263 cap_t *cap = NULL;
264 cap_handle_t handle;
265
266 /*
267 * First of all, see if we can reclaim a capability. Note that this
268 * feature is only temporary and capability reclamaition will eventually
269 * be phased out.
270 */
271 mutex_lock(&task->cap_info->lock);
272 hash_table_apply(&task->cap_info->caps, cap_reclaimer, &cap);
273
274 /*
275 * If we don't have a capability by now, try to allocate a new one.
276 */
277 if (!cap) {
278 cap = slab_alloc(cap_slab, FRAME_ATOMIC);
279 if (!cap) {
280 mutex_unlock(&task->cap_info->lock);
281 return ENOMEM;
282 }
283 uintptr_t hbase;
284 if (!ra_alloc(task->cap_info->handles, 1, 1, &hbase)) {
285 slab_free(cap_slab, cap);
286 mutex_unlock(&task->cap_info->lock);
287 return ENOMEM;
288 }
289 cap_initialize(cap, task, (cap_handle_t) hbase);
290 hash_table_insert(&task->cap_info->caps, &cap->caps_link);
291 }
292
293 cap->state = CAP_STATE_ALLOCATED;
294 handle = cap->handle;
295 mutex_unlock(&task->cap_info->lock);
296
297 return handle;
298}
299
300/** Publish allocated capability
301 *
302 * The kernel object is moved into the capability. In other words, its reference
303 * is handed over to the capability. Once published, userspace can access and
304 * manipulate the capability.
305 *
306 * @param task Task in which to publish the capability.
307 * @param handle Capability handle.
308 * @param kobj Kernel object.
309 */
310void
311cap_publish(task_t *task, cap_handle_t handle, kobject_t *kobj)
312{
313 mutex_lock(&task->cap_info->lock);
314 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
315 assert(cap);
316 cap->state = CAP_STATE_PUBLISHED;
317 /* Hand over kobj's reference to cap */
318 cap->kobject = kobj;
319 list_append(&cap->type_link, &task->cap_info->type_list[kobj->type]);
320 mutex_unlock(&task->cap_info->lock);
321}
322
323/** Unpublish published capability
324 *
325 * The kernel object is moved out of the capability. In other words, the
326 * capability's reference to the objects is handed over to the kernel object
327 * pointer returned by this function. Once unpublished, the capability does not
328 * refer to any kernel object anymore.
329 *
330 * @param task Task in which to unpublish the capability.
331 * @param handle Capability handle.
332 * @param type Kernel object type of the object associated with the
333 * capability.
334 */
335kobject_t *cap_unpublish(task_t *task, cap_handle_t handle, kobject_type_t type)
336{
337 kobject_t *kobj = NULL;
338
339 mutex_lock(&task->cap_info->lock);
340 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
341 if (cap) {
342 if (cap->kobject->type == type) {
343 /* Hand over cap's reference to kobj */
344 kobj = cap->kobject;
345 cap->kobject = NULL;
346 list_remove(&cap->type_link);
347 cap->state = CAP_STATE_ALLOCATED;
348 }
349 }
350 mutex_unlock(&task->cap_info->lock);
351
352 return kobj;
353}
354
355/** Free allocated capability
356 *
357 * @param task Task in which to free the capability.
358 * @param handle Capability handle.
359 */
360void cap_free(task_t *task, cap_handle_t handle)
361{
362 assert(handle >= CAPS_START);
363 assert(handle <= CAPS_LAST);
364
365 mutex_lock(&task->cap_info->lock);
366 cap_t *cap = cap_get(task, handle, CAP_STATE_ALLOCATED);
367
368 assert(cap);
369
370 hash_table_remove_item(&task->cap_info->caps, &cap->caps_link);
371 ra_free(task->cap_info->handles, handle, 1);
372 slab_free(cap_slab, cap);
373 mutex_unlock(&task->cap_info->lock);
374}
375
376/** Initialize kernel object
377 *
378 * @param kobj Kernel object to initialize.
379 * @param type Type of the kernel object.
380 * @param raw Raw pointer to the encapsulated object.
381 * @param ops Pointer to kernel object operations for the respective type.
382 */
383void kobject_initialize(kobject_t *kobj, kobject_type_t type, void *raw,
384 kobject_ops_t *ops)
385{
386 atomic_set(&kobj->refcnt, 1);
387 kobj->type = type;
388 kobj->raw = raw;
389 kobj->ops = ops;
390}
391
392/** Get new reference to kernel object from capability
393 *
394 * @param task Task from which to get the reference.
395 * @param handle Capability handle.
396 * @param type Kernel object type of the object associated with the
397 * capability referenced by handle.
398 *
399 * @return Kernel object with incremented reference count on success.
400 * @return NULL if there is no matching capability or kernel object.
401 */
402kobject_t *
403kobject_get(struct task *task, cap_handle_t handle, kobject_type_t type)
404{
405 kobject_t *kobj = NULL;
406
407 mutex_lock(&task->cap_info->lock);
408 cap_t *cap = cap_get(task, handle, CAP_STATE_PUBLISHED);
409 if (cap) {
410 if (cap->kobject->type == type) {
411 kobj = cap->kobject;
412 atomic_inc(&kobj->refcnt);
413 }
414 }
415 mutex_unlock(&task->cap_info->lock);
416
417 return kobj;
418}
419
420/** Record new reference
421 *
422 * @param kobj Kernel object from which the new reference is created.
423 */
424void kobject_add_ref(kobject_t *kobj)
425{
426 atomic_inc(&kobj->refcnt);
427}
428
429/** Drop reference to kernel object
430 *
431 * The encapsulated object and the kobject_t wrapper are both destroyed when the
432 * last reference is dropped.
433 *
434 * @param kobj Kernel object whose reference to drop.
435 */
436void kobject_put(kobject_t *kobj)
437{
438 if (atomic_postdec(&kobj->refcnt) == 1) {
439 kobj->ops->destroy(kobj->raw);
440 free(kobj);
441 }
442}
443
444/** @}
445 */
Note: See TracBrowser for help on using the repository browser.