[acc0efb] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Jan Vesely
|
---|
| 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 libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <assert.h>
|
---|
| 36 | #include <errno.h>
|
---|
[38d150e] | 37 | #include <stdlib.h>
|
---|
[acc0efb] | 38 | #include <adt/list.h>
|
---|
| 39 | #include <fibril_synch.h>
|
---|
| 40 | #include <ddi.h>
|
---|
| 41 | #include <str.h>
|
---|
| 42 |
|
---|
| 43 | typedef struct {
|
---|
| 44 | link_t link;
|
---|
| 45 | volatile void *base;
|
---|
| 46 | size_t size;
|
---|
| 47 | void *data;
|
---|
| 48 | trace_fnc log;
|
---|
| 49 | } region_t;
|
---|
| 50 |
|
---|
[1433ecda] | 51 | static inline region_t *region_instance(link_t *l)
|
---|
[acc0efb] | 52 | {
|
---|
| 53 | return list_get_instance(l, region_t, link);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[1433ecda] | 56 | static inline region_t *region_create(volatile void *base, size_t size,
|
---|
| 57 | trace_fnc log, void *data)
|
---|
[acc0efb] | 58 | {
|
---|
| 59 | region_t *new_reg = malloc(sizeof(region_t));
|
---|
| 60 | if (new_reg) {
|
---|
| 61 | link_initialize(&new_reg->link);
|
---|
| 62 | new_reg->base = base;
|
---|
| 63 | new_reg->data = data;
|
---|
| 64 | new_reg->size = size;
|
---|
| 65 | new_reg->log = log;
|
---|
| 66 | }
|
---|
| 67 | return new_reg;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | static inline void region_destroy(region_t *r)
|
---|
| 71 | {
|
---|
| 72 | free(r);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | typedef struct {
|
---|
| 76 | list_t list;
|
---|
| 77 | fibril_rwlock_t guard;
|
---|
| 78 | } pio_regions_t;
|
---|
| 79 |
|
---|
[1433ecda] | 80 | static pio_regions_t *get_regions(void)
|
---|
[acc0efb] | 81 | {
|
---|
| 82 | static pio_regions_t regions = {
|
---|
| 83 | .list = {
|
---|
| 84 | .head = { ®ions.list.head, ®ions.list.head },
|
---|
| 85 | },
|
---|
| 86 | .guard = FIBRIL_RWLOCK_INITIALIZER(regions.guard),
|
---|
| 87 | };
|
---|
| 88 | return ®ions;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[aa537a5a] | 91 | void pio_trace_log(const volatile void *r, uint64_t val, bool write)
|
---|
[acc0efb] | 92 | {
|
---|
| 93 | pio_regions_t *regions = get_regions();
|
---|
| 94 | fibril_rwlock_read_lock(®ions->guard);
|
---|
[feeac0d] | 95 | list_foreach(regions->list, link, region_t, reg) {
|
---|
[acc0efb] | 96 | if ((r >= reg->base) && (r < reg->base + reg->size)) {
|
---|
| 97 | if (reg->log)
|
---|
| 98 | reg->log(r, val, reg->base,
|
---|
| 99 | reg->size, reg->data, write);
|
---|
| 100 | break;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | fibril_rwlock_read_unlock(®ions->guard);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[b7fd2a0] | 106 | errno_t pio_trace_enable(void *base, size_t size, trace_fnc log, void *data)
|
---|
[acc0efb] | 107 | {
|
---|
| 108 | pio_regions_t *regions = get_regions();
|
---|
| 109 | assert(regions);
|
---|
| 110 |
|
---|
| 111 | region_t *region = region_create(base, size, log, data);
|
---|
| 112 | if (!region)
|
---|
| 113 | return ENOMEM;
|
---|
| 114 |
|
---|
| 115 | fibril_rwlock_write_lock(®ions->guard);
|
---|
| 116 | list_append(®ion->link, ®ions->list);
|
---|
| 117 | fibril_rwlock_write_unlock(®ions->guard);
|
---|
| 118 | return EOK;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void pio_trace_disable(void *r)
|
---|
| 122 | {
|
---|
| 123 | pio_regions_t *regions = get_regions();
|
---|
| 124 | assert(regions);
|
---|
| 125 |
|
---|
| 126 | fibril_rwlock_write_lock(®ions->guard);
|
---|
| 127 | list_foreach_safe(regions->list, it, next) {
|
---|
| 128 | region_t *reg = region_instance(it);
|
---|
| 129 | if (r >= reg->base && (r < reg->base + reg->size)) {
|
---|
[feeac0d] | 130 | list_remove(®->link);
|
---|
| 131 | region_destroy(reg);
|
---|
[acc0efb] | 132 | }
|
---|
| 133 | }
|
---|
| 134 | fibril_rwlock_write_unlock(®ions->guard);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** @}
|
---|
| 138 | */
|
---|