CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
stdio.h
1// Copyright Microsoft and CHERIoT Contributors.
2// SPDX-License-Identifier: MIT
3
4#ifndef __STDIO_H__
5#define __STDIO_H__
6
7#include <cdefs.h>
9#include <stdarg.h>
10#include <stddef.h>
11#include <cheri-builtins.h>
12
13#define PRT_MAX_SIZE (0x80)
14#define EOF (-1)
15
16__BEGIN_DECLS
17
18/**
19 * This is a very simple implementation of a subset of stdio and supports only
20 * UARTs. The Uart type is often a C++ template type and so we can't forward
21 * declare it in a C header and so we use a volatile void* instead, which can
22 * be cast to the correct type inside the library.
23 */
24typedef volatile void FILE;
25
26#if DEVICE_EXISTS(uart0)
27# define stdout MMIO_CAPABILITY(void, uart0)
28# define stdin MMIO_CAPABILITY(void, uart0)
29#elif DEVICE_EXISTS(uart)
30# define stdout MMIO_CAPABILITY(void, uart)
31# define stdin MMIO_CAPABILITY(void uart)
32#else
33#error No device found for stdout and stderr
34#endif
35
36#if DEVICE_EXISTS(uart1) && !STDERR_TO_STDOUT
37# define stderr MMIO_CAPABILITY(void, uart1)
38#elif defined(stdout)
39# define stderr stdout
40#else
41#error No device found for stderr
42#endif
43
44int __cheri_libcall vfprintf(FILE *stream, const char *fmt, va_list ap);
45
46static inline int fprintf(FILE *stream, const char *format, ...)
47{
48 va_list ap;
49
50 va_start(ap, format);
51 int ret = vfprintf(stream, format, ap);
52 va_end(ap);
53 return ret;
54}
55
56#ifdef stdout
57static inline int printf(const char *format, ...)
58{
59 va_list ap;
60
61 va_start(ap, format);
62 int ret = vfprintf(stdout, format, ap);
63 va_end(ap);
64 return ret;
65}
66#endif
67
68int __cheri_libcall snprintf(char *str, size_t size, const char *format, ...);
69int __cheri_libcall vsnprintf(char *str,
70 size_t size,
71 const char *format,
72 va_list ap);
73
74static inline int sprintf(char *str, const char *format, ...)
75{
76 va_list args;
77 va_start(args, format);
78 int ret = vsnprintf(str, cheri_top_get(str) - cheri_address_get(str), format, args);
79 va_end(args);
80 return ret;
81}
82__END_DECLS
83
84#endif /* !__STDIO_H__ */
Macros for interacting with the compartmentalisation model in CHERIoT.