blob: 992bad02e4fc91ae2a0462322d9c4d649e07dd38 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01007 */
8
Andrew Scullfbc938a2018-08-20 14:09:28 +01009#pragma once
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010010
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010011#include <stdarg.h>
Andrew Walbran7f904bf2019-07-12 16:38:38 +010012#include <stddef.h>
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010013
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010014#include "hf/ffa.h"
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010015
Andrew Walbranf3ac84d2019-07-26 16:10:02 +010016#define DLOG_BUFFER_SIZE 8192
17
Karl Meakine8fdaed2024-05-22 10:02:21 +010018enum log_level {
19 LOG_LEVEL_NONE = 0,
20 LOG_LEVEL_ERROR = 1,
21 LOG_LEVEL_NOTICE = 2,
22 LOG_LEVEL_WARNING = 3,
23 LOG_LEVEL_INFO = 4,
24 LOG_LEVEL_VERBOSE = 5,
25};
Andrew Walbran17eebf92020-02-05 16:35:49 +000026
Andrew Walbranf3ac84d2019-07-26 16:10:02 +010027extern size_t dlog_buffer_offset;
28extern char dlog_buffer[];
29
Karl Meakin1978e7a2024-11-20 13:56:58 +000030/**
31 * This struct is a workaround to allow passing `va_list` by pointer on x86_64.
32 * On x86_64, `va_list` is an array, and in C arrays are weird.
33 *
34 * In particular, function parameters of array types are really pointers: the
35 * functions `void f(char[1])` and `void f(char*)` are identical (see
36 * https://en.cppreference.com/w/c/language/array). But this does not apply to
37 * function parameters of type pointer to array: the functions `void
38 * f(char(*)[1])` and `void f(char**)` are not identical.
39 *
40 * Therefore in the body of `by_value`, `args` has type `__va_list_tag *`.
41 * The call to `by_pointer` will cause a compile error, as `&args` has type
42 * `va_list_tag **` but `by_pointer` expects `va_list (*)[1]`.
43 *
44 * ```
45 * typedef va_list __va_list_tag[1];
46 *
47 * void by_pointer(va_list *args) {}
48 *
49 * void by_value(va_list args) {
50 * by_pointer(&args);
51 * }
52 * ```
53 *
54 * The workaround to prevent array to pointer decay is to wrap the array in a
55 * struct, since structs do not decay to pointers.
56 */
57struct va_list_wrapper {
58 va_list va;
59};
60
Andrew Scullfdd716e2018-12-20 05:37:31 +000061void dlog_enable_lock(void);
Karl Meakin54454902024-04-02 16:27:06 +010062__attribute__((format(printf, 1, 2))) size_t dlog(const char *fmt, ...);
Karl Meakin1978e7a2024-11-20 13:56:58 +000063size_t vdlog(const char *fmt, struct va_list_wrapper *args);
Andrew Walbran17eebf92020-02-05 16:35:49 +000064
Demi Marie Obenour33f8a162023-02-14 12:21:41 -050065/*
66 * The do { ... } while (0) syntax is used to ensure that callers of
67 * these macros follow them with a semicolon.
68 *
69 * Run-time conditionals are preferred over preprocessor conditionals to ensure
70 * that the code is type-checked and linted unconditionally, even if it will not
71 * be executed at run-time. Logging statements that are disabled at
72 * compile-time are unreachable code and will be eliminated by compiler
73 * optimizations.
74 */
75#define dlog_error(...) \
76 do { \
77 if (LOG_LEVEL >= LOG_LEVEL_ERROR) { \
78 dlog("ERROR: " __VA_ARGS__); \
79 } \
80 } while (0)
Andrew Walbran17eebf92020-02-05 16:35:49 +000081
Demi Marie Obenour33f8a162023-02-14 12:21:41 -050082#define dlog_notice(...) \
83 do { \
84 if (LOG_LEVEL >= LOG_LEVEL_NOTICE) { \
85 dlog("NOTICE: " __VA_ARGS__); \
86 } \
87 } while (0)
Andrew Walbran17eebf92020-02-05 16:35:49 +000088
Demi Marie Obenour33f8a162023-02-14 12:21:41 -050089#define dlog_warning(...) \
90 do { \
91 if (LOG_LEVEL >= LOG_LEVEL_WARNING) { \
92 dlog("WARNING: " __VA_ARGS__); \
93 } \
94 } while (0)
Andrew Walbran17eebf92020-02-05 16:35:49 +000095
Demi Marie Obenour33f8a162023-02-14 12:21:41 -050096#define dlog_info(...) \
97 do { \
98 if (LOG_LEVEL >= LOG_LEVEL_WARNING) { \
99 dlog("INFO: " __VA_ARGS__); \
100 } \
101 } while (0)
Andrew Walbran17eebf92020-02-05 16:35:49 +0000102
Demi Marie Obenour33f8a162023-02-14 12:21:41 -0500103#define dlog_verbose(...) \
104 do { \
105 if (LOG_LEVEL >= LOG_LEVEL_VERBOSE) { \
106 dlog("VERBOSE: " __VA_ARGS__); \
107 } \
108 } while (0)