| [91db0280] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2013 Martin Sucha
|
|---|
| 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 |
|
|---|
| [174156fd] | 29 | /** @addtogroup kernel_generic
|
|---|
| [91db0280] | 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| [163e34c] | 35 | #include <abi/log.h>
|
|---|
| 36 | #include <arch.h>
|
|---|
| 37 | #include <atomic.h>
|
|---|
| 38 | #include <console/console.h>
|
|---|
| [91db0280] | 39 | #include <ddi/ddi.h>
|
|---|
| [163e34c] | 40 | #include <ddi/irq.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| [91db0280] | 42 | #include <ipc/event.h>
|
|---|
| 43 | #include <ipc/irq.h>
|
|---|
| [163e34c] | 44 | #include <log.h>
|
|---|
| [91db0280] | 45 | #include <panic.h>
|
|---|
| 46 | #include <print.h>
|
|---|
| [163e34c] | 47 | #include <printf_core.h>
|
|---|
| 48 | #include <putchar.h>
|
|---|
| [91db0280] | 49 | #include <stdarg.h>
|
|---|
| [aafed15] | 50 | #include <stdlib.h>
|
|---|
| [163e34c] | 51 | #include <str.h>
|
|---|
| 52 | #include <synch/spinlock.h>
|
|---|
| 53 | #include <syscall/copy.h>
|
|---|
| 54 | #include <sysinfo/sysinfo.h>
|
|---|
| 55 | #include <typedefs.h>
|
|---|
| [91db0280] | 56 |
|
|---|
| 57 | #define LOG_PAGES 8
|
|---|
| 58 | #define LOG_LENGTH (LOG_PAGES * PAGE_SIZE)
|
|---|
| 59 | #define LOG_ENTRY_HEADER_LENGTH (sizeof(size_t) + sizeof(uint32_t))
|
|---|
| 60 |
|
|---|
| 61 | /** Cyclic buffer holding the data for kernel log */
|
|---|
| 62 | uint8_t log_buffer[LOG_LENGTH] __attribute__((aligned(PAGE_SIZE)));
|
|---|
| 63 |
|
|---|
| 64 | /** Kernel log initialized */
|
|---|
| [e90cfa6] | 65 | static atomic_bool log_inited = false;
|
|---|
| [91db0280] | 66 |
|
|---|
| 67 | /** Position in the cyclic buffer where the first log entry starts */
|
|---|
| 68 | size_t log_start = 0;
|
|---|
| 69 |
|
|---|
| 70 | /** Sum of length of all log entries currently stored in the cyclic buffer */
|
|---|
| 71 | size_t log_used = 0;
|
|---|
| 72 |
|
|---|
| 73 | /** Log spinlock */
|
|---|
| 74 | SPINLOCK_STATIC_INITIALIZE_NAME(log_lock, "log_lock");
|
|---|
| 75 |
|
|---|
| 76 | /** Overall count of logged messages, which may overflow as needed */
|
|---|
| 77 | static uint32_t log_counter = 0;
|
|---|
| 78 |
|
|---|
| 79 | /** Starting position of the entry currently being written to the log */
|
|---|
| 80 | static size_t log_current_start = 0;
|
|---|
| 81 |
|
|---|
| 82 | /** Length (including header) of the entry currently being written to the log */
|
|---|
| 83 | static size_t log_current_len = 0;
|
|---|
| 84 |
|
|---|
| 85 | /** Start of the next entry to be handed to uspace starting from log_start */
|
|---|
| 86 | static size_t next_for_uspace = 0;
|
|---|
| 87 |
|
|---|
| 88 | static void log_update(void *);
|
|---|
| 89 |
|
|---|
| 90 | /** Initialize kernel logging facility
|
|---|
| 91 | *
|
|---|
| 92 | */
|
|---|
| 93 | void log_init(void)
|
|---|
| [1b20da0] | 94 | {
|
|---|
| [91db0280] | 95 | event_set_unmask_callback(EVENT_KLOG, log_update);
|
|---|
| [e3306d04] | 96 | atomic_store(&log_inited, true);
|
|---|
| [91db0280] | 97 | }
|
|---|
| 98 |
|
|---|
| [ae7d03c] | 99 | static size_t log_copy_from(uint8_t *data, size_t pos, size_t len)
|
|---|
| 100 | {
|
|---|
| [91db0280] | 101 | for (size_t i = 0; i < len; i++, pos = (pos + 1) % LOG_LENGTH) {
|
|---|
| 102 | data[i] = log_buffer[pos];
|
|---|
| 103 | }
|
|---|
| 104 | return pos;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| [ae7d03c] | 107 | static size_t log_copy_to(const uint8_t *data, size_t pos, size_t len)
|
|---|
| 108 | {
|
|---|
| [91db0280] | 109 | for (size_t i = 0; i < len; i++, pos = (pos + 1) % LOG_LENGTH) {
|
|---|
| 110 | log_buffer[pos] = data[i];
|
|---|
| 111 | }
|
|---|
| 112 | return pos;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /** Append data to the currently open log entry.
|
|---|
| [1b20da0] | 116 | *
|
|---|
| [91db0280] | 117 | * This function requires that the log_lock is acquired by the caller.
|
|---|
| 118 | */
|
|---|
| 119 | static void log_append(const uint8_t *data, size_t len)
|
|---|
| 120 | {
|
|---|
| 121 | /* Cap the length so that the entry entirely fits into the buffer */
|
|---|
| 122 | if (len > LOG_LENGTH - log_current_len) {
|
|---|
| 123 | len = LOG_LENGTH - log_current_len;
|
|---|
| 124 | }
|
|---|
| [a35b458] | 125 |
|
|---|
| [91db0280] | 126 | if (len == 0)
|
|---|
| 127 | return;
|
|---|
| [a35b458] | 128 |
|
|---|
| [91db0280] | 129 | size_t log_free = LOG_LENGTH - log_used - log_current_len;
|
|---|
| [a35b458] | 130 |
|
|---|
| [91db0280] | 131 | /* Discard older entries to make space, if necessary */
|
|---|
| 132 | while (len > log_free) {
|
|---|
| 133 | size_t entry_len;
|
|---|
| 134 | log_copy_from((uint8_t *) &entry_len, log_start, sizeof(size_t));
|
|---|
| 135 | log_start = (log_start + entry_len) % LOG_LENGTH;
|
|---|
| 136 | log_used -= entry_len;
|
|---|
| 137 | log_free += entry_len;
|
|---|
| 138 | next_for_uspace -= entry_len;
|
|---|
| 139 | }
|
|---|
| [a35b458] | 140 |
|
|---|
| [91db0280] | 141 | size_t pos = (log_current_start + log_current_len) % LOG_LENGTH;
|
|---|
| 142 | log_copy_to(data, pos, len);
|
|---|
| 143 | log_current_len += len;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /** Begin writing an entry to the log.
|
|---|
| [1b20da0] | 147 | *
|
|---|
| [91db0280] | 148 | * This acquires the log and output buffer locks, so only calls to log_* functions should
|
|---|
| 149 | * be used until calling log_end.
|
|---|
| 150 | */
|
|---|
| 151 | void log_begin(log_facility_t fac, log_level_t level)
|
|---|
| 152 | {
|
|---|
| [90dd8aee] | 153 | console_lock();
|
|---|
| [91db0280] | 154 | spinlock_lock(&log_lock);
|
|---|
| 155 | spinlock_lock(&kio_lock);
|
|---|
| [a35b458] | 156 |
|
|---|
| [91db0280] | 157 | log_current_start = (log_start + log_used) % LOG_LENGTH;
|
|---|
| 158 | log_current_len = 0;
|
|---|
| [a35b458] | 159 |
|
|---|
| [91db0280] | 160 | /* Write header of the log entry, the length will be written in log_end() */
|
|---|
| 161 | log_append((uint8_t *) &log_current_len, sizeof(size_t));
|
|---|
| 162 | log_append((uint8_t *) &log_counter, sizeof(uint32_t));
|
|---|
| 163 | uint32_t fac32 = fac;
|
|---|
| 164 | uint32_t lvl32 = level;
|
|---|
| 165 | log_append((uint8_t *) &fac32, sizeof(uint32_t));
|
|---|
| 166 | log_append((uint8_t *) &lvl32, sizeof(uint32_t));
|
|---|
| [a35b458] | 167 |
|
|---|
| [91db0280] | 168 | log_counter++;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /** Finish writing an entry to the log.
|
|---|
| [1b20da0] | 172 | *
|
|---|
| [91db0280] | 173 | * This releases the log and output buffer locks.
|
|---|
| 174 | */
|
|---|
| [ae7d03c] | 175 | void log_end(void)
|
|---|
| 176 | {
|
|---|
| [91db0280] | 177 | /* Set the length in the header to correct value */
|
|---|
| 178 | log_copy_to((uint8_t *) &log_current_len, log_current_start, sizeof(size_t));
|
|---|
| 179 | log_used += log_current_len;
|
|---|
| [a35b458] | 180 |
|
|---|
| [91db0280] | 181 | kio_push_char('\n');
|
|---|
| 182 | spinlock_unlock(&kio_lock);
|
|---|
| 183 | spinlock_unlock(&log_lock);
|
|---|
| [a35b458] | 184 |
|
|---|
| [91db0280] | 185 | /* This has to be called after we released the locks above */
|
|---|
| 186 | kio_flush();
|
|---|
| 187 | kio_update(NULL);
|
|---|
| 188 | log_update(NULL);
|
|---|
| [90dd8aee] | 189 | console_unlock();
|
|---|
| [91db0280] | 190 | }
|
|---|
| 191 |
|
|---|
| 192 | static void log_update(void *event)
|
|---|
| 193 | {
|
|---|
| [036e97c] | 194 | if (!atomic_load(&log_inited))
|
|---|
| [91db0280] | 195 | return;
|
|---|
| [a35b458] | 196 |
|
|---|
| [91db0280] | 197 | spinlock_lock(&log_lock);
|
|---|
| 198 | if (next_for_uspace < log_used)
|
|---|
| 199 | event_notify_0(EVENT_KLOG, true);
|
|---|
| 200 | spinlock_unlock(&log_lock);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | static int log_printf_str_write(const char *str, size_t size, void *data)
|
|---|
| 204 | {
|
|---|
| 205 | size_t offset = 0;
|
|---|
| [a35b458] | 206 |
|
|---|
| [163e34c] | 207 | while (offset < size)
|
|---|
| [91db0280] | 208 | kio_push_char(str_decode(str, &offset, size));
|
|---|
| [a35b458] | 209 |
|
|---|
| [91db0280] | 210 | log_append((const uint8_t *)str, size);
|
|---|
| [a35b458] | 211 |
|
|---|
| [163e34c] | 212 | return EOK;
|
|---|
| [91db0280] | 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /** Append a message to the currently being written entry.
|
|---|
| [1b20da0] | 216 | *
|
|---|
| [91db0280] | 217 | * Requires that an entry has been started using log_begin()
|
|---|
| 218 | */
|
|---|
| 219 | int log_vprintf(const char *fmt, va_list args)
|
|---|
| 220 | {
|
|---|
| 221 | printf_spec_t ps = {
|
|---|
| 222 | log_printf_str_write,
|
|---|
| 223 | NULL
|
|---|
| 224 | };
|
|---|
| [a35b458] | 225 |
|
|---|
| [163e34c] | 226 | return printf_core(fmt, &ps, args);
|
|---|
| [91db0280] | 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /** Append a message to the currently being written entry.
|
|---|
| [1b20da0] | 230 | *
|
|---|
| [91db0280] | 231 | * Requires that an entry has been started using log_begin()
|
|---|
| 232 | */
|
|---|
| 233 | int log_printf(const char *fmt, ...)
|
|---|
| 234 | {
|
|---|
| 235 | int ret;
|
|---|
| 236 | va_list args;
|
|---|
| [a35b458] | 237 |
|
|---|
| [91db0280] | 238 | va_start(args, fmt);
|
|---|
| 239 | ret = log_vprintf(fmt, args);
|
|---|
| 240 | va_end(args);
|
|---|
| [a35b458] | 241 |
|
|---|
| [91db0280] | 242 | return ret;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /** Log a message to the kernel log.
|
|---|
| [1b20da0] | 246 | *
|
|---|
| [91db0280] | 247 | * This atomically appends a log entry.
|
|---|
| 248 | * The resulting message should not contain a trailing newline, as the log
|
|---|
| 249 | * entries are explicitly delimited when stored in the log.
|
|---|
| 250 | */
|
|---|
| 251 | int log(log_facility_t fac, log_level_t level, const char *fmt, ...)
|
|---|
| 252 | {
|
|---|
| 253 | int ret;
|
|---|
| 254 | va_list args;
|
|---|
| [a35b458] | 255 |
|
|---|
| [91db0280] | 256 | log_begin(fac, level);
|
|---|
| [a35b458] | 257 |
|
|---|
| [91db0280] | 258 | va_start(args, fmt);
|
|---|
| 259 | ret = log_vprintf(fmt, args);
|
|---|
| 260 | va_end(args);
|
|---|
| [a35b458] | 261 |
|
|---|
| [91db0280] | 262 | log_end();
|
|---|
| [a35b458] | 263 |
|
|---|
| [91db0280] | 264 | return ret;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /** Control of the log from uspace
|
|---|
| 268 | *
|
|---|
| 269 | */
|
|---|
| [5a5269d] | 270 | sys_errno_t sys_klog(sysarg_t operation, uspace_addr_t buf, size_t size,
|
|---|
| 271 | sysarg_t level, uspace_ptr_size_t uspace_nread)
|
|---|
| [91db0280] | 272 | {
|
|---|
| 273 | char *data;
|
|---|
| [b7fd2a0] | 274 | errno_t rc;
|
|---|
| [a35b458] | 275 |
|
|---|
| [91db0280] | 276 | if (size > PAGE_SIZE)
|
|---|
| [b7fd2a0] | 277 | return (sys_errno_t) ELIMIT;
|
|---|
| [a35b458] | 278 |
|
|---|
| [91db0280] | 279 | switch (operation) {
|
|---|
| [ae7d03c] | 280 | case KLOG_WRITE:
|
|---|
| [11b285d] | 281 | data = (char *) malloc(size + 1);
|
|---|
| [ae7d03c] | 282 | if (!data)
|
|---|
| 283 | return (sys_errno_t) ENOMEM;
|
|---|
| 284 |
|
|---|
| 285 | rc = copy_from_uspace(data, buf, size);
|
|---|
| 286 | if (rc) {
|
|---|
| 287 | free(data);
|
|---|
| 288 | return (sys_errno_t) rc;
|
|---|
| 289 | }
|
|---|
| 290 | data[size] = 0;
|
|---|
| [a35b458] | 291 |
|
|---|
| [ae7d03c] | 292 | if (level >= LVL_LIMIT)
|
|---|
| 293 | level = LVL_NOTE;
|
|---|
| [a35b458] | 294 |
|
|---|
| [ae7d03c] | 295 | log(LF_USPACE, level, "%s", data);
|
|---|
| [a35b458] | 296 |
|
|---|
| [ae7d03c] | 297 | free(data);
|
|---|
| 298 | return EOK;
|
|---|
| 299 | case KLOG_READ:
|
|---|
| [11b285d] | 300 | data = (char *) malloc(size);
|
|---|
| [ae7d03c] | 301 | if (!data)
|
|---|
| 302 | return (sys_errno_t) ENOMEM;
|
|---|
| 303 |
|
|---|
| 304 | size_t entry_len = 0;
|
|---|
| 305 | size_t copied = 0;
|
|---|
| 306 |
|
|---|
| 307 | rc = EOK;
|
|---|
| 308 |
|
|---|
| 309 | spinlock_lock(&log_lock);
|
|---|
| 310 |
|
|---|
| 311 | while (next_for_uspace < log_used) {
|
|---|
| 312 | size_t pos = (log_start + next_for_uspace) % LOG_LENGTH;
|
|---|
| 313 | log_copy_from((uint8_t *) &entry_len, pos, sizeof(size_t));
|
|---|
| 314 |
|
|---|
| 315 | if (entry_len > PAGE_SIZE) {
|
|---|
| 316 | /*
|
|---|
| 317 | * Since we limit data transfer
|
|---|
| 318 | * to uspace to a maximum of PAGE_SIZE
|
|---|
| 319 | * bytes, skip any entries larger
|
|---|
| 320 | * than this limit to prevent
|
|---|
| 321 | * userspace being stuck trying to
|
|---|
| 322 | * read them.
|
|---|
| 323 | */
|
|---|
| [91db0280] | 324 | next_for_uspace += entry_len;
|
|---|
| [ae7d03c] | 325 | continue;
|
|---|
| [91db0280] | 326 | }
|
|---|
| [a35b458] | 327 |
|
|---|
| [ae7d03c] | 328 | if (size < copied + entry_len) {
|
|---|
| 329 | if (copied == 0)
|
|---|
| 330 | rc = EOVERFLOW;
|
|---|
| 331 | break;
|
|---|
| [91db0280] | 332 | }
|
|---|
| [a35b458] | 333 |
|
|---|
| [ae7d03c] | 334 | log_copy_from((uint8_t *) (data + copied), pos, entry_len);
|
|---|
| 335 | copied += entry_len;
|
|---|
| 336 | next_for_uspace += entry_len;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | spinlock_unlock(&log_lock);
|
|---|
| [a35b458] | 340 |
|
|---|
| [ae7d03c] | 341 | if (rc != EOK) {
|
|---|
| [91db0280] | 342 | free(data);
|
|---|
| [ae7d03c] | 343 | return (sys_errno_t) rc;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | rc = copy_to_uspace(buf, data, size);
|
|---|
| 347 |
|
|---|
| 348 | free(data);
|
|---|
| [a35b458] | 349 |
|
|---|
| [ae7d03c] | 350 | if (rc != EOK)
|
|---|
| 351 | return (sys_errno_t) rc;
|
|---|
| [a35b458] | 352 |
|
|---|
| [ae7d03c] | 353 | return copy_to_uspace(uspace_nread, &copied, sizeof(copied));
|
|---|
| 354 | return EOK;
|
|---|
| 355 | default:
|
|---|
| 356 | return (sys_errno_t) ENOTSUP;
|
|---|
| [91db0280] | 357 | }
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | /** @}
|
|---|
| 361 | */
|
|---|