1/**
2 * \file
3 * \brief Kernel logging infrastructure headers.
4 *
5 * All C source in the kernel should include this file first.
6 * This file should contain only definitions and prototypes that are
7 * required for the majority of kernel code.
8 */
9
10/*
11 * Copyright (c) 2016, ETH Zurich.
12 * All rights reserved.
13 *
14 * This file is distributed under the terms in the attached LICENSE file.
15 * If you do not find this file, copies can be found by writing to:
16 * ETH Zurich D-INFK, Universitaetstr. 6, CH-8092 Zurich. Attn: Systems Group.
17 */
18
19#ifndef __KERNEL_LOGGING_H
20#define __KERNEL_LOGGING_H
21
22#include <assert.h>
23#include <stddef.h>
24#include <stdio.h> // printf for debug
25#include <stdint.h>
26#include <inttypes.h>
27#include <stdbool.h>
28#include <barrelfish_kpi/types.h>
29#include <barrelfish_kpi/cpu.h>
30#include <barrelfish_kpi/registers_arch.h>
31#include <errors/errno.h>
32#include <bitmacros.h>
33#include <debug.h>
34#include <offsets.h> /* XXX */
35#include <schedule.h>
36
37
38#define DEFAULT_LOGLEVEL        LOG_NOTE
39#define DEFAULT_SUBSYSTEM_MASK  (~0L)
40
41
42/**
43 * Kernel subsystems.
44 */
45enum KERNEL_SUBSYSTEM {
46    SUBSYS_STARTUP        = (1 << 0),        ///< Startup
47    SUBSYS_GDB            = (1 << 1),        ///< GDB stub
48    SUBSYS_APIC           = (1 << 2),        ///< APIC driver
49    SUBSYS_ELF            = (1 << 3),        ///< ELF64 loader
50    SUBSYS_PAGING         = (1 << 4),        ///< Paging
51    SUBSYS_SYSCALL        = (1 << 5),        ///< System calls
52    SUBSYS_CAPS           = (1 << 6),        ///< Capabilities
53    SUBSYS_DISPATCH       = (1 << 7),        ///< Scheduling and dispatch
54    SUBSYS_IO             = (1 << 8),        ///< Low-level IO operations
55};
56/**
57 * Kernel message loglevels.
58 */
59enum KERNEL_LOG_LEVEL {
60    LOG_PANIC      = 0,       ///< Panic
61    LOG_ERR        = 1,       ///< Error
62    LOG_WARN       = 2,       ///< Warning
63    LOG_NOTE       = 3,       ///< Notice
64    LOG_DEBUG      = 4,       ///< Debug
65};
66
67void panic(const char *, ...)
68    __attribute__((noreturn, format(printf, 1, 2)));
69void printk(int level, const char *msg, ...)
70    __attribute__ ((format(printf, 2, 3)));
71int printf_nolog(const char * fmt, ...)
72    __attribute__ ((format(printf, 1, 2)));
73
74/**
75 * Command-line variable to set kernel logging level.
76 */
77extern int kernel_loglevel;
78
79/**
80 * Command-line variable to control which subsystems log. Bits defined
81 * SUBSYS_* definitions in this file.
82 */
83extern int kernel_log_subsystem_mask;
84
85/**
86 * \brief Log a kernel debug message.
87 *
88 * Logs printf()-style debug message 'fmt' from subsystem 'subs'
89 * to the default kernel console(s). Additional arguments are like
90 * printf(). Whether the message is put out depends on the current
91 * kernel log level, as well as on the current kernel subsystem log
92 * mask.  'debug' is a macro so that the cost of marshalling the
93 * arguments is avoided if the relevant debugging is disabled.
94 *
95 * \param _subs     Subsystem this message stems from.
96 * \param _fmt      The message (printf() format string)
97 */
98
99#define debug(_subs, _fmt, ...) \
100do { \
101    if (((_subs) & kernel_log_subsystem_mask) && (kernel_loglevel >= LOG_DEBUG)) \
102        printk(LOG_DEBUG, _fmt, ## __VA_ARGS__); \
103} while(0)
104
105#endif // __KERNEL_LOGGING_H
106