| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Jakub Vana
|
|---|
| 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 | #include <sysinfo/sysinfo.h>
|
|---|
| 36 | #include <mm/slab.h>
|
|---|
| 37 | #include <print.h>
|
|---|
| 38 | #include <syscall/copy.h>
|
|---|
| 39 | #include <synch/spinlock.h>
|
|---|
| 40 | #include <arch/asm.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 |
|
|---|
| 43 | /** Maximal sysinfo path length */
|
|---|
| 44 | #define SYSINFO_MAX_PATH 2048
|
|---|
| 45 |
|
|---|
| 46 | bool fb_exported = false;
|
|---|
| 47 |
|
|---|
| 48 | /** Global sysinfo tree root item */
|
|---|
| 49 | static sysinfo_item_t *global_root = NULL;
|
|---|
| 50 |
|
|---|
| 51 | /** Sysinfo SLAB cache */
|
|---|
| 52 | static slab_cache_t *sysinfo_item_slab;
|
|---|
| 53 |
|
|---|
| 54 | /** Sysinfo spinlock */
|
|---|
| 55 | SPINLOCK_STATIC_INITIALIZE_NAME(sysinfo_lock, "sysinfo_lock");
|
|---|
| 56 |
|
|---|
| 57 | /** Sysinfo item constructor
|
|---|
| 58 | *
|
|---|
| 59 | */
|
|---|
| 60 | static int sysinfo_item_constructor(void *obj, int kmflag)
|
|---|
| 61 | {
|
|---|
| 62 | sysinfo_item_t *item = (sysinfo_item_t *) obj;
|
|---|
| 63 |
|
|---|
| 64 | item->name = NULL;
|
|---|
| 65 | item->val_type = SYSINFO_VAL_UNDEFINED;
|
|---|
| 66 | item->subtree_type = SYSINFO_SUBTREE_NONE;
|
|---|
| 67 | item->subtree.table = NULL;
|
|---|
| 68 | item->next = NULL;
|
|---|
| 69 |
|
|---|
| 70 | return 0;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Sysinfo item destructor
|
|---|
| 74 | *
|
|---|
| 75 | * Note that the return value is not perfectly correct
|
|---|
| 76 | * since more space might get actually freed thanks
|
|---|
| 77 | * to the disposal of item->name
|
|---|
| 78 | *
|
|---|
| 79 | */
|
|---|
| 80 | static int sysinfo_item_destructor(void *obj)
|
|---|
| 81 | {
|
|---|
| 82 | sysinfo_item_t *item = (sysinfo_item_t *) obj;
|
|---|
| 83 |
|
|---|
| 84 | if (item->name != NULL)
|
|---|
| 85 | free(item->name);
|
|---|
| 86 |
|
|---|
| 87 | return 0;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /** Initialize sysinfo subsystem
|
|---|
| 91 | *
|
|---|
| 92 | * Create SLAB cache for sysinfo items.
|
|---|
| 93 | *
|
|---|
| 94 | */
|
|---|
| 95 | void sysinfo_init(void)
|
|---|
| 96 | {
|
|---|
| 97 | sysinfo_item_slab = slab_cache_create("sysinfo_item_slab",
|
|---|
| 98 | sizeof(sysinfo_item_t), 0, sysinfo_item_constructor,
|
|---|
| 99 | sysinfo_item_destructor, SLAB_CACHE_MAGDEFERRED);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /** Recursively find an item in sysinfo tree
|
|---|
| 103 | *
|
|---|
| 104 | * Should be called with interrupts disabled
|
|---|
| 105 | * and sysinfo_lock held.
|
|---|
| 106 | *
|
|---|
| 107 | * @param name Current sysinfo path suffix.
|
|---|
| 108 | * @param subtree Current sysinfo (sub)tree root item.
|
|---|
| 109 | * @param ret If the return value is NULL, this argument
|
|---|
| 110 | * can be either also NULL (i.e. no item was
|
|---|
| 111 | * found and no data was generated) or the
|
|---|
| 112 | * original pointer is used to store the value
|
|---|
| 113 | * generated by a generated subtree function.
|
|---|
| 114 | *
|
|---|
| 115 | * @return Found item or NULL if no item in the fixed tree
|
|---|
| 116 | * was found (N.B. ret).
|
|---|
| 117 | *
|
|---|
| 118 | */
|
|---|
| 119 | static sysinfo_item_t *sysinfo_find_item(const char *name,
|
|---|
| 120 | sysinfo_item_t *subtree, sysinfo_return_t **ret)
|
|---|
| 121 | {
|
|---|
| 122 | ASSERT(subtree != NULL);
|
|---|
| 123 | ASSERT(ret != NULL);
|
|---|
| 124 |
|
|---|
| 125 | sysinfo_item_t *cur = subtree;
|
|---|
| 126 |
|
|---|
| 127 | /* Walk all siblings */
|
|---|
| 128 | while (cur != NULL) {
|
|---|
| 129 | size_t i = 0;
|
|---|
| 130 |
|
|---|
| 131 | /* Compare name with path */
|
|---|
| 132 | while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
|
|---|
| 133 | i++;
|
|---|
| 134 |
|
|---|
| 135 | /* Check for perfect name and path match */
|
|---|
| 136 | if ((name[i] == 0) && (cur->name[i] == 0))
|
|---|
| 137 | return cur;
|
|---|
| 138 |
|
|---|
| 139 | /* Partial match up to the delimiter */
|
|---|
| 140 | if ((name[i] == '.') && (cur->name[i] == 0)) {
|
|---|
| 141 | /* Look into the subtree */
|
|---|
| 142 | switch (cur->subtree_type) {
|
|---|
| 143 | case SYSINFO_SUBTREE_TABLE:
|
|---|
| 144 | /* Recursively find in subtree */
|
|---|
| 145 | return sysinfo_find_item(name + i + 1,
|
|---|
| 146 | cur->subtree.table, ret);
|
|---|
| 147 | case SYSINFO_SUBTREE_FUNCTION:
|
|---|
| 148 | /* Get generated data */
|
|---|
| 149 | **ret = cur->subtree.get_data(name + i + 1);
|
|---|
| 150 | return NULL;
|
|---|
| 151 | default:
|
|---|
| 152 | /* Not found, no data generated */
|
|---|
| 153 | *ret = NULL;
|
|---|
| 154 | return NULL;
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | cur = cur->next;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /* Not found, no data generated */
|
|---|
| 162 | *ret = NULL;
|
|---|
| 163 | return NULL;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** Recursively create items in sysinfo tree
|
|---|
| 167 | *
|
|---|
| 168 | * Should be called with interrupts disabled
|
|---|
| 169 | * and sysinfo_lock held.
|
|---|
| 170 | *
|
|---|
| 171 | * @param name Current sysinfo path suffix.
|
|---|
| 172 | * @param psubtree Pointer to an already existing (sub)tree root
|
|---|
| 173 | * item or where to store a new tree root item.
|
|---|
| 174 | *
|
|---|
| 175 | * @return Existing or newly allocated sysinfo item or NULL
|
|---|
| 176 | * if the current tree configuration does not allow to
|
|---|
| 177 | * create a new item.
|
|---|
| 178 | *
|
|---|
| 179 | */
|
|---|
| 180 | static sysinfo_item_t *sysinfo_create_path(const char *name,
|
|---|
| 181 | sysinfo_item_t **psubtree)
|
|---|
| 182 | {
|
|---|
| 183 | ASSERT(psubtree != NULL);
|
|---|
| 184 |
|
|---|
| 185 | if (*psubtree == NULL) {
|
|---|
| 186 | /* No parent */
|
|---|
| 187 |
|
|---|
| 188 | size_t i = 0;
|
|---|
| 189 |
|
|---|
| 190 | /* Find the first delimiter in name */
|
|---|
| 191 | while ((name[i] != 0) && (name[i] != '.'))
|
|---|
| 192 | i++;
|
|---|
| 193 |
|
|---|
| 194 | *psubtree =
|
|---|
| 195 | (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
|
|---|
| 196 | ASSERT(*psubtree);
|
|---|
| 197 |
|
|---|
| 198 | /* Fill in item name up to the delimiter */
|
|---|
| 199 | (*psubtree)->name = str_ndup(name, i);
|
|---|
| 200 | ASSERT((*psubtree)->name);
|
|---|
| 201 |
|
|---|
| 202 | /* Create subtree items */
|
|---|
| 203 | if (name[i] == '.') {
|
|---|
| 204 | (*psubtree)->subtree_type = SYSINFO_SUBTREE_TABLE;
|
|---|
| 205 | return sysinfo_create_path(name + i + 1,
|
|---|
| 206 | &((*psubtree)->subtree.table));
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /* No subtree needs to be created */
|
|---|
| 210 | return *psubtree;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | sysinfo_item_t *cur = *psubtree;
|
|---|
| 214 |
|
|---|
| 215 | /* Walk all siblings */
|
|---|
| 216 | while (cur != NULL) {
|
|---|
| 217 | size_t i = 0;
|
|---|
| 218 |
|
|---|
| 219 | /* Compare name with path */
|
|---|
| 220 | while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
|
|---|
| 221 | i++;
|
|---|
| 222 |
|
|---|
| 223 | /* Check for perfect name and path match
|
|---|
| 224 | * -> item is already present.
|
|---|
| 225 | */
|
|---|
| 226 | if ((name[i] == 0) && (cur->name[i] == 0))
|
|---|
| 227 | return cur;
|
|---|
| 228 |
|
|---|
| 229 | /* Partial match up to the delimiter */
|
|---|
| 230 | if ((name[i] == '.') && (cur->name[i] == 0)) {
|
|---|
| 231 | switch (cur->subtree_type) {
|
|---|
| 232 | case SYSINFO_SUBTREE_NONE:
|
|---|
| 233 | /* No subtree yet, create one */
|
|---|
| 234 | cur->subtree_type = SYSINFO_SUBTREE_TABLE;
|
|---|
| 235 | return sysinfo_create_path(name + i + 1,
|
|---|
| 236 | &(cur->subtree.table));
|
|---|
| 237 | case SYSINFO_SUBTREE_TABLE:
|
|---|
| 238 | /* Subtree already created, add new sibling */
|
|---|
| 239 | return sysinfo_create_path(name + i + 1,
|
|---|
| 240 | &(cur->subtree.table));
|
|---|
| 241 | default:
|
|---|
| 242 | /* Subtree items handled by a function, this
|
|---|
| 243 | * cannot be overriden by a constant item.
|
|---|
| 244 | */
|
|---|
| 245 | return NULL;
|
|---|
| 246 | }
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | /* No match and no more siblings to check
|
|---|
| 250 | * -> create a new sibling item.
|
|---|
| 251 | */
|
|---|
| 252 | if (cur->next == NULL) {
|
|---|
| 253 | /* Find the first delimiter in name */
|
|---|
| 254 | i = 0;
|
|---|
| 255 | while ((name[i] != 0) && (name[i] != '.'))
|
|---|
| 256 | i++;
|
|---|
| 257 |
|
|---|
| 258 | sysinfo_item_t *item =
|
|---|
| 259 | (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
|
|---|
| 260 | ASSERT(item);
|
|---|
| 261 |
|
|---|
| 262 | cur->next = item;
|
|---|
| 263 |
|
|---|
| 264 | /* Fill in item name up to the delimiter */
|
|---|
| 265 | item->name = str_ndup(name, i);
|
|---|
| 266 | ASSERT(item->name);
|
|---|
| 267 |
|
|---|
| 268 | /* Create subtree items */
|
|---|
| 269 | if (name[i] == '.') {
|
|---|
| 270 | item->subtree_type = SYSINFO_SUBTREE_TABLE;
|
|---|
| 271 | return sysinfo_create_path(name + i + 1,
|
|---|
| 272 | &(item->subtree.table));
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /* No subtree needs to be created */
|
|---|
| 276 | return item;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | cur = cur->next;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | /* Unreachable */
|
|---|
| 283 | ASSERT(false);
|
|---|
| 284 | return NULL;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /** Set sysinfo item with a constant numeric value
|
|---|
| 288 | *
|
|---|
| 289 | * @param name Sysinfo path.
|
|---|
| 290 | * @param root Pointer to the root item or where to store
|
|---|
| 291 | * a new root item (NULL for global sysinfo root).
|
|---|
| 292 | * @param val Value to store in the item.
|
|---|
| 293 | *
|
|---|
| 294 | */
|
|---|
| 295 | void sysinfo_set_item_val(const char *name, sysinfo_item_t **root,
|
|---|
| 296 | unative_t val)
|
|---|
| 297 | {
|
|---|
| 298 | /* Protect sysinfo tree consistency */
|
|---|
| 299 | ipl_t ipl = interrupts_disable();
|
|---|
| 300 | spinlock_lock(&sysinfo_lock);
|
|---|
| 301 |
|
|---|
| 302 | if (root == NULL)
|
|---|
| 303 | root = &global_root;
|
|---|
| 304 |
|
|---|
| 305 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| 306 | if (item != NULL) {
|
|---|
| 307 | item->val_type = SYSINFO_VAL_VAL;
|
|---|
| 308 | item->val.val = val;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 312 | interrupts_restore(ipl);
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /** Set sysinfo item with a constant binary data
|
|---|
| 316 | *
|
|---|
| 317 | * Note that sysinfo only stores the pointer to the
|
|---|
| 318 | * binary data and does not touch it in any way. The
|
|---|
| 319 | * data should be static and immortal.
|
|---|
| 320 | *
|
|---|
| 321 | * @param name Sysinfo path.
|
|---|
| 322 | * @param root Pointer to the root item or where to store
|
|---|
| 323 | * a new root item (NULL for global sysinfo root).
|
|---|
| 324 | * @param data Binary data.
|
|---|
| 325 | * @param size Size of the binary data.
|
|---|
| 326 | *
|
|---|
| 327 | */
|
|---|
| 328 | void sysinfo_set_item_data(const char *name, sysinfo_item_t **root,
|
|---|
| 329 | void *data, size_t size)
|
|---|
| 330 | {
|
|---|
| 331 | /* Protect sysinfo tree consistency */
|
|---|
| 332 | ipl_t ipl = interrupts_disable();
|
|---|
| 333 | spinlock_lock(&sysinfo_lock);
|
|---|
| 334 |
|
|---|
| 335 | if (root == NULL)
|
|---|
| 336 | root = &global_root;
|
|---|
| 337 |
|
|---|
| 338 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| 339 | if (item != NULL) {
|
|---|
| 340 | item->val_type = SYSINFO_VAL_DATA;
|
|---|
| 341 | item->val.data.data = data;
|
|---|
| 342 | item->val.data.size = size;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 346 | interrupts_restore(ipl);
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | /** Set sysinfo item with a generated numeric value
|
|---|
| 350 | *
|
|---|
| 351 | * @param name Sysinfo path.
|
|---|
| 352 | * @param root Pointer to the root item or where to store
|
|---|
| 353 | * a new root item (NULL for global sysinfo root).
|
|---|
| 354 | * @param fn Numeric value generator function.
|
|---|
| 355 | *
|
|---|
| 356 | */
|
|---|
| 357 | void sysinfo_set_item_fn_val(const char *name, sysinfo_item_t **root,
|
|---|
| 358 | sysinfo_fn_val_t fn)
|
|---|
| 359 | {
|
|---|
| 360 | /* Protect sysinfo tree consistency */
|
|---|
| 361 | ipl_t ipl = interrupts_disable();
|
|---|
| 362 | spinlock_lock(&sysinfo_lock);
|
|---|
| 363 |
|
|---|
| 364 | if (root == NULL)
|
|---|
| 365 | root = &global_root;
|
|---|
| 366 |
|
|---|
| 367 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| 368 | if (item != NULL) {
|
|---|
| 369 | item->val_type = SYSINFO_VAL_FUNCTION_VAL;
|
|---|
| 370 | item->val.fn_val = fn;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 374 | interrupts_restore(ipl);
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /** Set sysinfo item with a generated binary data
|
|---|
| 378 | *
|
|---|
| 379 | * Note that each time the generator function is called
|
|---|
| 380 | * it is supposed to return a new dynamically allocated
|
|---|
| 381 | * data. This data is then freed by sysinfo in the context
|
|---|
| 382 | * of the current sysinfo request.
|
|---|
| 383 | *
|
|---|
| 384 | * @param name Sysinfo path.
|
|---|
| 385 | * @param root Pointer to the root item or where to store
|
|---|
| 386 | * a new root item (NULL for global sysinfo root).
|
|---|
| 387 | * @param fn Binary data generator function.
|
|---|
| 388 | *
|
|---|
| 389 | */
|
|---|
| 390 | void sysinfo_set_item_fn_data(const char *name, sysinfo_item_t **root,
|
|---|
| 391 | sysinfo_fn_data_t fn)
|
|---|
| 392 | {
|
|---|
| 393 | /* Protect sysinfo tree consistency */
|
|---|
| 394 | ipl_t ipl = interrupts_disable();
|
|---|
| 395 | spinlock_lock(&sysinfo_lock);
|
|---|
| 396 |
|
|---|
| 397 | if (root == NULL)
|
|---|
| 398 | root = &global_root;
|
|---|
| 399 |
|
|---|
| 400 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| 401 | if (item != NULL) {
|
|---|
| 402 | item->val_type = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 403 | item->val.fn_data = fn;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 407 | interrupts_restore(ipl);
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | /** Set sysinfo item with an undefined value
|
|---|
| 411 | *
|
|---|
| 412 | * @param name Sysinfo path.
|
|---|
| 413 | * @param root Pointer to the root item or where to store
|
|---|
| 414 | * a new root item (NULL for global sysinfo root).
|
|---|
| 415 | *
|
|---|
| 416 | */
|
|---|
| 417 | void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
|
|---|
| 418 | {
|
|---|
| 419 | /* Protect sysinfo tree consistency */
|
|---|
| 420 | ipl_t ipl = interrupts_disable();
|
|---|
| 421 | spinlock_lock(&sysinfo_lock);
|
|---|
| 422 |
|
|---|
| 423 | if (root == NULL)
|
|---|
| 424 | root = &global_root;
|
|---|
| 425 |
|
|---|
| 426 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| 427 | if (item != NULL)
|
|---|
| 428 | item->val_type = SYSINFO_VAL_UNDEFINED;
|
|---|
| 429 |
|
|---|
| 430 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 431 | interrupts_restore(ipl);
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | /** Set sysinfo item with a generated subtree
|
|---|
| 435 | *
|
|---|
| 436 | * @param name Sysinfo path.
|
|---|
| 437 | * @param root Pointer to the root item or where to store
|
|---|
| 438 | * a new root item (NULL for global sysinfo root).
|
|---|
| 439 | * @param fn Subtree generator function.
|
|---|
| 440 | *
|
|---|
| 441 | */
|
|---|
| 442 | void sysinfo_set_subtree_fn(const char *name, sysinfo_item_t **root,
|
|---|
| 443 | sysinfo_fn_subtree_t fn)
|
|---|
| 444 | {
|
|---|
| 445 | /* Protect sysinfo tree consistency */
|
|---|
| 446 | ipl_t ipl = interrupts_disable();
|
|---|
| 447 | spinlock_lock(&sysinfo_lock);
|
|---|
| 448 |
|
|---|
| 449 | if (root == NULL)
|
|---|
| 450 | root = &global_root;
|
|---|
| 451 |
|
|---|
| 452 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| 453 |
|
|---|
| 454 | /* Change the type of the subtree only if it is not already
|
|---|
| 455 | a fixed subtree */
|
|---|
| 456 | if ((item != NULL) && (item->subtree_type != SYSINFO_SUBTREE_TABLE)) {
|
|---|
| 457 | item->subtree_type = SYSINFO_SUBTREE_FUNCTION;
|
|---|
| 458 | item->subtree.get_data = fn;
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 462 | interrupts_restore(ipl);
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | /** Sysinfo dump indentation helper routine
|
|---|
| 466 | *
|
|---|
| 467 | * @param depth Number of indentation characters to print.
|
|---|
| 468 | *
|
|---|
| 469 | */
|
|---|
| 470 | static void sysinfo_indent(unsigned int depth)
|
|---|
| 471 | {
|
|---|
| 472 | unsigned int i;
|
|---|
| 473 | for (i = 0; i < depth; i++)
|
|---|
| 474 | printf(" ");
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | /** Dump the structure of sysinfo tree
|
|---|
| 478 | *
|
|---|
| 479 | * Should be called with interrupts disabled
|
|---|
| 480 | * and sysinfo_lock held. Because this routine
|
|---|
| 481 | * might take a reasonable long time to proceed,
|
|---|
| 482 | * having the spinlock held is not optimal, but
|
|---|
| 483 | * there is no better simple solution.
|
|---|
| 484 | *
|
|---|
| 485 | * @param root Root item of the current (sub)tree.
|
|---|
| 486 | * @param depth Current depth in the sysinfo tree.
|
|---|
| 487 | *
|
|---|
| 488 | */
|
|---|
| 489 | static void sysinfo_dump_internal(sysinfo_item_t *root, unsigned int depth)
|
|---|
| 490 | {
|
|---|
| 491 | sysinfo_item_t *cur = root;
|
|---|
| 492 |
|
|---|
| 493 | /* Walk all siblings */
|
|---|
| 494 | while (cur != NULL) {
|
|---|
| 495 | sysinfo_indent(depth);
|
|---|
| 496 |
|
|---|
| 497 | unative_t val;
|
|---|
| 498 | void *data;
|
|---|
| 499 | size_t size;
|
|---|
| 500 |
|
|---|
| 501 | /* Display node value and type */
|
|---|
| 502 | switch (cur->val_type) {
|
|---|
| 503 | case SYSINFO_VAL_UNDEFINED:
|
|---|
| 504 | printf("+ %s\n", cur->name);
|
|---|
| 505 | break;
|
|---|
| 506 | case SYSINFO_VAL_VAL:
|
|---|
| 507 | printf("+ %s -> %" PRIun" (%#" PRIxn ")\n", cur->name,
|
|---|
| 508 | cur->val.val, cur->val.val);
|
|---|
| 509 | break;
|
|---|
| 510 | case SYSINFO_VAL_DATA:
|
|---|
| 511 | printf("+ %s (%" PRIs" bytes)\n", cur->name,
|
|---|
| 512 | cur->val.data.size);
|
|---|
| 513 | break;
|
|---|
| 514 | case SYSINFO_VAL_FUNCTION_VAL:
|
|---|
| 515 | val = cur->val.fn_val(cur);
|
|---|
| 516 | printf("+ %s -> %" PRIun" (%#" PRIxn ") [generated]\n",
|
|---|
| 517 | cur->name, val, val);
|
|---|
| 518 | break;
|
|---|
| 519 | case SYSINFO_VAL_FUNCTION_DATA:
|
|---|
| 520 | data = cur->val.fn_data(cur, &size);
|
|---|
| 521 |
|
|---|
| 522 | /* N.B.: The generated binary data should be freed */
|
|---|
| 523 | if (data != NULL)
|
|---|
| 524 | free(data);
|
|---|
| 525 |
|
|---|
| 526 | printf("+ %s (%" PRIs" bytes) [generated]\n", cur->name,
|
|---|
| 527 | size);
|
|---|
| 528 | break;
|
|---|
| 529 | default:
|
|---|
| 530 | printf("+ %s [unknown]\n", cur->name);
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | /* Recursivelly nest into the subtree */
|
|---|
| 534 | switch (cur->subtree_type) {
|
|---|
| 535 | case SYSINFO_SUBTREE_NONE:
|
|---|
| 536 | break;
|
|---|
| 537 | case SYSINFO_SUBTREE_TABLE:
|
|---|
| 538 | sysinfo_dump_internal(cur->subtree.table, depth + 1);
|
|---|
| 539 | break;
|
|---|
| 540 | case SYSINFO_SUBTREE_FUNCTION:
|
|---|
| 541 | sysinfo_indent(depth + 1);
|
|---|
| 542 | printf("+ [generated subtree]\n");
|
|---|
| 543 | break;
|
|---|
| 544 | default:
|
|---|
| 545 | sysinfo_indent(depth + 1);
|
|---|
| 546 | printf("+ [unknown subtree]\n");
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | cur = cur->next;
|
|---|
| 550 | }
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | /** Dump the structure of sysinfo tree
|
|---|
| 554 | *
|
|---|
| 555 | * @param root Root item of the sysinfo (sub)tree.
|
|---|
| 556 | * If it is NULL then consider the global
|
|---|
| 557 | * sysinfo tree.
|
|---|
| 558 | *
|
|---|
| 559 | */
|
|---|
| 560 | void sysinfo_dump(sysinfo_item_t *root)
|
|---|
| 561 | {
|
|---|
| 562 | /* Avoid other functions to mess with sysinfo
|
|---|
| 563 | while we are dumping it */
|
|---|
| 564 | ipl_t ipl = interrupts_disable();
|
|---|
| 565 | spinlock_lock(&sysinfo_lock);
|
|---|
| 566 |
|
|---|
| 567 | if (root == NULL)
|
|---|
| 568 | sysinfo_dump_internal(global_root, 0);
|
|---|
| 569 | else
|
|---|
| 570 | sysinfo_dump_internal(root, 0);
|
|---|
| 571 |
|
|---|
| 572 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 573 | interrupts_restore(ipl);
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | /** Return sysinfo item value determined by name
|
|---|
| 577 | *
|
|---|
| 578 | * Should be called with interrupts disabled
|
|---|
| 579 | * and sysinfo_lock held.
|
|---|
| 580 | *
|
|---|
| 581 | * @param name Sysinfo path.
|
|---|
| 582 | * @param root Root item of the sysinfo (sub)tree.
|
|---|
| 583 | * If it is NULL then consider the global
|
|---|
| 584 | * sysinfo tree.
|
|---|
| 585 | *
|
|---|
| 586 | * @return Item value (constant or generated).
|
|---|
| 587 | *
|
|---|
| 588 | */
|
|---|
| 589 | static sysinfo_return_t sysinfo_get_item(const char *name, sysinfo_item_t **root)
|
|---|
| 590 | {
|
|---|
| 591 | if (root == NULL)
|
|---|
| 592 | root = &global_root;
|
|---|
| 593 |
|
|---|
| 594 | /* Try to find the item or generate data */
|
|---|
| 595 | sysinfo_return_t ret;
|
|---|
| 596 | sysinfo_return_t *ret_ptr = &ret;
|
|---|
| 597 | sysinfo_item_t *item = sysinfo_find_item(name, *root, &ret_ptr);
|
|---|
| 598 |
|
|---|
| 599 | if (item != NULL) {
|
|---|
| 600 | /* Item found in the fixed sysinfo tree */
|
|---|
| 601 |
|
|---|
| 602 | ret.tag = item->val_type;
|
|---|
| 603 | switch (item->val_type) {
|
|---|
| 604 | case SYSINFO_VAL_UNDEFINED:
|
|---|
| 605 | break;
|
|---|
| 606 | case SYSINFO_VAL_VAL:
|
|---|
| 607 | ret.val = item->val.val;
|
|---|
| 608 | break;
|
|---|
| 609 | case SYSINFO_VAL_DATA:
|
|---|
| 610 | ret.data = item->val.data;
|
|---|
| 611 | break;
|
|---|
| 612 | case SYSINFO_VAL_FUNCTION_VAL:
|
|---|
| 613 | ret.val = item->val.fn_val(item);
|
|---|
| 614 | break;
|
|---|
| 615 | case SYSINFO_VAL_FUNCTION_DATA:
|
|---|
| 616 | ret.data.data = item->val.fn_data(item, &ret.data.size);
|
|---|
| 617 | break;
|
|---|
| 618 | }
|
|---|
| 619 | } else {
|
|---|
| 620 | /* No item in the fixed sysinfo tree */
|
|---|
| 621 | if (ret_ptr == NULL) {
|
|---|
| 622 | /* Even no data was generated */
|
|---|
| 623 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 624 | }
|
|---|
| 625 | }
|
|---|
| 626 |
|
|---|
| 627 | return ret;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | /** Return sysinfo item determined by name from user space
|
|---|
| 631 | *
|
|---|
| 632 | * Should be called with interrupts disabled
|
|---|
| 633 | * and sysinfo_lock held. The path string passed from
|
|---|
| 634 | * the user space has to be properly null-terminated
|
|---|
| 635 | * (the last passed character must be null).
|
|---|
| 636 | *
|
|---|
| 637 | * @param ptr Sysinfo path in the user address space.
|
|---|
| 638 | * @param size Size of the path string.
|
|---|
| 639 | *
|
|---|
| 640 | */
|
|---|
| 641 | static sysinfo_return_t sysinfo_get_item_uspace(void *ptr, size_t size)
|
|---|
| 642 | {
|
|---|
| 643 | sysinfo_return_t ret;
|
|---|
| 644 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 645 |
|
|---|
| 646 | if (size > SYSINFO_MAX_PATH)
|
|---|
| 647 | return ret;
|
|---|
| 648 |
|
|---|
| 649 | char *path = (char *) malloc(size + 1, 0);
|
|---|
| 650 | ASSERT(path);
|
|---|
| 651 |
|
|---|
| 652 | if ((copy_from_uspace(path, ptr, size + 1) == 0)
|
|---|
| 653 | && (path[size] == 0))
|
|---|
| 654 | ret = sysinfo_get_item(path, NULL);
|
|---|
| 655 |
|
|---|
| 656 | free(path);
|
|---|
| 657 | return ret;
|
|---|
| 658 | }
|
|---|
| 659 |
|
|---|
| 660 | /** Get the sysinfo value type (syscall)
|
|---|
| 661 | *
|
|---|
| 662 | * The path string passed from the user space has
|
|---|
| 663 | * to be properly null-terminated (the last passed
|
|---|
| 664 | * character must be null).
|
|---|
| 665 | *
|
|---|
| 666 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 667 | * @param path_size Size of the path string.
|
|---|
| 668 | *
|
|---|
| 669 | * @return Item value type.
|
|---|
| 670 | *
|
|---|
| 671 | */
|
|---|
| 672 | unative_t sys_sysinfo_get_tag(void *path_ptr, size_t path_size)
|
|---|
| 673 | {
|
|---|
| 674 | /* Avoid other functions to mess with sysinfo
|
|---|
| 675 | while we are reading it */
|
|---|
| 676 | ipl_t ipl = interrupts_disable();
|
|---|
| 677 | spinlock_lock(&sysinfo_lock);
|
|---|
| 678 |
|
|---|
| 679 | /* Get the item */
|
|---|
| 680 | sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size);
|
|---|
| 681 |
|
|---|
| 682 | /* N.B.: The generated binary data should be freed */
|
|---|
| 683 | if ((ret.tag == SYSINFO_VAL_FUNCTION_DATA) && (ret.data.data != NULL))
|
|---|
| 684 | free(ret.data.data);
|
|---|
| 685 |
|
|---|
| 686 | /* Map generated value types to constant types
|
|---|
| 687 | (user space does not care whether the
|
|---|
| 688 | value is constant or generated) */
|
|---|
| 689 | if (ret.tag == SYSINFO_VAL_FUNCTION_VAL)
|
|---|
| 690 | ret.tag = SYSINFO_VAL_VAL;
|
|---|
| 691 | else if (ret.tag == SYSINFO_VAL_FUNCTION_DATA)
|
|---|
| 692 | ret.tag = SYSINFO_VAL_DATA;
|
|---|
| 693 |
|
|---|
| 694 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 695 | interrupts_restore(ipl);
|
|---|
| 696 |
|
|---|
| 697 | return (unative_t) ret.tag;
|
|---|
| 698 | }
|
|---|
| 699 |
|
|---|
| 700 | /** Get the sysinfo numerical value (syscall)
|
|---|
| 701 | *
|
|---|
| 702 | * The path string passed from the user space has
|
|---|
| 703 | * to be properly null-terminated (the last passed
|
|---|
| 704 | * character must be null).
|
|---|
| 705 | *
|
|---|
| 706 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 707 | * @param path_size Size of the path string.
|
|---|
| 708 | * @param value_ptr User space pointer where to store the
|
|---|
| 709 | * numberical value.
|
|---|
| 710 | *
|
|---|
| 711 | * @return Error code (EOK in case of no error).
|
|---|
| 712 | *
|
|---|
| 713 | */
|
|---|
| 714 | unative_t sys_sysinfo_get_value(void *path_ptr, size_t path_size,
|
|---|
| 715 | void *value_ptr)
|
|---|
| 716 | {
|
|---|
| 717 | /* Avoid other functions to mess with sysinfo
|
|---|
| 718 | while we are reading it */
|
|---|
| 719 | ipl_t ipl = interrupts_disable();
|
|---|
| 720 | spinlock_lock(&sysinfo_lock);
|
|---|
| 721 |
|
|---|
| 722 | /* Get the item */
|
|---|
| 723 | sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size);
|
|---|
| 724 | int rc;
|
|---|
| 725 |
|
|---|
| 726 | /* Only constant or generated numerical value is returned */
|
|---|
| 727 | if ((ret.tag == SYSINFO_VAL_VAL) || (ret.tag == SYSINFO_VAL_FUNCTION_VAL))
|
|---|
| 728 | rc = copy_to_uspace(value_ptr, &ret.val, sizeof(ret.val));
|
|---|
| 729 | else
|
|---|
| 730 | rc = EINVAL;
|
|---|
| 731 |
|
|---|
| 732 | /* N.B.: The generated binary data should be freed */
|
|---|
| 733 | if ((ret.tag == SYSINFO_VAL_FUNCTION_DATA) && (ret.data.data != NULL))
|
|---|
| 734 | free(ret.data.data);
|
|---|
| 735 |
|
|---|
| 736 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 737 | interrupts_restore(ipl);
|
|---|
| 738 |
|
|---|
| 739 | return (unative_t) rc;
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | /** Get the sysinfo binary data size (syscall)
|
|---|
| 743 | *
|
|---|
| 744 | * The path string passed from the user space has
|
|---|
| 745 | * to be properly null-terminated (the last passed
|
|---|
| 746 | * character must be null).
|
|---|
| 747 | *
|
|---|
| 748 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 749 | * @param path_size Size of the path string.
|
|---|
| 750 | * @param size_ptr User space pointer where to store the
|
|---|
| 751 | * binary data size.
|
|---|
| 752 | *
|
|---|
| 753 | * @return Error code (EOK in case of no error).
|
|---|
| 754 | *
|
|---|
| 755 | */
|
|---|
| 756 | unative_t sys_sysinfo_get_data_size(void *path_ptr, size_t path_size,
|
|---|
| 757 | void *size_ptr)
|
|---|
| 758 | {
|
|---|
| 759 | /* Avoid other functions to mess with sysinfo
|
|---|
| 760 | while we are reading it */
|
|---|
| 761 | ipl_t ipl = interrupts_disable();
|
|---|
| 762 | spinlock_lock(&sysinfo_lock);
|
|---|
| 763 |
|
|---|
| 764 | /* Get the item */
|
|---|
| 765 | sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size);
|
|---|
| 766 | int rc;
|
|---|
| 767 |
|
|---|
| 768 | /* Only the size of constant or generated binary data is considered */
|
|---|
| 769 | if ((ret.tag == SYSINFO_VAL_DATA) || (ret.tag == SYSINFO_VAL_FUNCTION_DATA))
|
|---|
| 770 | rc = copy_to_uspace(size_ptr, &ret.data.size,
|
|---|
| 771 | sizeof(ret.data.size));
|
|---|
| 772 | else
|
|---|
| 773 | rc = EINVAL;
|
|---|
| 774 |
|
|---|
| 775 | /* N.B.: The generated binary data should be freed */
|
|---|
| 776 | if ((ret.tag == SYSINFO_VAL_FUNCTION_DATA) && (ret.data.data != NULL))
|
|---|
| 777 | free(ret.data.data);
|
|---|
| 778 |
|
|---|
| 779 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 780 | interrupts_restore(ipl);
|
|---|
| 781 |
|
|---|
| 782 | return (unative_t) rc;
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | /** Get the sysinfo binary data (syscall)
|
|---|
| 786 | *
|
|---|
| 787 | * The path string passed from the user space has
|
|---|
| 788 | * to be properly null-terminated (the last passed
|
|---|
| 789 | * character must be null).
|
|---|
| 790 | *
|
|---|
| 791 | * The user space buffer must be sized exactly according
|
|---|
| 792 | * to the size of the binary data, otherwise the request
|
|---|
| 793 | * fails.
|
|---|
| 794 | *
|
|---|
| 795 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 796 | * @param path_size Size of the path string.
|
|---|
| 797 | * @param buffer_ptr User space pointer to the buffer where
|
|---|
| 798 | * to store the binary data.
|
|---|
| 799 | * @param buffer_size User space buffer size.
|
|---|
| 800 | *
|
|---|
| 801 | * @return Error code (EOK in case of no error).
|
|---|
| 802 | *
|
|---|
| 803 | */
|
|---|
| 804 | unative_t sys_sysinfo_get_data(void *path_ptr, size_t path_size,
|
|---|
| 805 | void *buffer_ptr, size_t buffer_size)
|
|---|
| 806 | {
|
|---|
| 807 | /* Avoid other functions to mess with sysinfo
|
|---|
| 808 | while we are reading it */
|
|---|
| 809 | ipl_t ipl = interrupts_disable();
|
|---|
| 810 | spinlock_lock(&sysinfo_lock);
|
|---|
| 811 |
|
|---|
| 812 | /* Get the item */
|
|---|
| 813 | sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size);
|
|---|
| 814 | int rc;
|
|---|
| 815 |
|
|---|
| 816 | /* Only constant or generated binary data is considered */
|
|---|
| 817 | if ((ret.tag == SYSINFO_VAL_DATA) || (ret.tag == SYSINFO_VAL_FUNCTION_DATA)) {
|
|---|
| 818 | /* Check destination buffer size */
|
|---|
| 819 | if (ret.data.size == buffer_size)
|
|---|
| 820 | rc = copy_to_uspace(buffer_ptr, ret.data.data,
|
|---|
| 821 | ret.data.size);
|
|---|
| 822 | else
|
|---|
| 823 | rc = ENOMEM;
|
|---|
| 824 | } else
|
|---|
| 825 | rc = EINVAL;
|
|---|
| 826 |
|
|---|
| 827 | /* N.B.: The generated binary data should be freed */
|
|---|
| 828 | if ((ret.tag == SYSINFO_VAL_FUNCTION_DATA) && (ret.data.data != NULL))
|
|---|
| 829 | free(ret.data.data);
|
|---|
| 830 |
|
|---|
| 831 | spinlock_unlock(&sysinfo_lock);
|
|---|
| 832 | interrupts_restore(ipl);
|
|---|
| 833 |
|
|---|
| 834 | return (unative_t) rc;
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 | /** @}
|
|---|
| 838 | */
|
|---|