MOS Source Code
Loading...
Searching...
No Matches
console.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
5
6#include <array>
7#include <limits.h>
9#include <mos/io/io.hpp>
12#include <mos/syslog/printk.hpp>
14#include <mos/tasks/wait.hpp>
15#include <mos_string.hpp>
16
18
19std::array<Console *, 128> console_list = {};
20
22{
23 if (c == 0x3)
24 {
27 {
28 Thread *thread = thread_get(entry->waiter);
29 if (thread)
30 signal_send_to_thread(thread, SIGINT);
31 }
33 }
34
36 waitlist_wake(&waitlist, INT_MAX);
37}
38
39static size_t console_io_read(io_t *io, void *data, size_t size)
40{
41 Console *con = container_of(io, Console, io);
42
43retry_read:;
44 size_t read = 0;
45
48 {
50 bool ok = reschedule_for_waitlist(&con->waitlist);
51 if (!ok)
52 {
53 pr_emerg("console: '%s' closed", con->name);
54 return -EIO;
55 }
57
59 {
61 return -ERESTARTSYS;
62 }
63 }
64 else
65 {
66 read = ring_buffer_pos_pop_front(con->reader.buf, &con->reader.pos, (u8 *) data, size);
67 }
69
70 if (read == 0)
71 goto retry_read;
72
73 return read;
74}
75
76static size_t console_io_write(io_t *io, const void *data, size_t size)
77{
78 Console *con = container_of(io, Console, io);
80 if ((con->caps & CONSOLE_CAP_COLOR))
81 con->set_color(con->default_fg, con->default_bg);
82 size_t ret = con->do_write((const char *) data, size);
84 return ret;
85}
86
87static const io_op_t console_io_ops = {
88 .read = console_io_read,
89 .write = console_io_write,
90};
91
93{
94 bool result = con->extra_setup();
95 if (!result)
96 {
97 pr_emerg("console: failed to setup '%s'", con->name);
98 return;
99 }
100
101 MOS_ASSERT_X(con->name != NULL, "console: %p's name is NULL", con);
102
103 io_flags_t flags = IO_WRITABLE;
104
105 if (con->caps & CONSOLE_CAP_READ)
106 {
107 MOS_ASSERT_X(con->reader.buf, "console: '%s' has no read buffer", con->name);
109 flags |= IO_READABLE;
110 }
111
112 io_init(&con->io, IO_CONSOLE, flags, &console_io_ops);
114 waitlist_init(&con->waitlist);
115}
116
118{
120 return NULL;
121
123 {
124 if (strcmp(con->name, name) == 0)
125 return con;
126 }
127 return NULL;
128}
129
130Console *console_get_by_prefix(const char *prefix)
131{
133 {
134 if (strncmp(con->name, prefix, strlen(prefix)) == 0)
135 return con;
136 }
137 return NULL;
138}
#define MOS_ASSERT_X(cond, msg,...)
Definition assert.hpp:15
Console * console_get_by_prefix(const char *prefix)
Definition console.cpp:130
static size_t console_io_read(io_t *io, void *data, size_t size)
Definition console.cpp:39
void console_register(Console *con)
Definition console.cpp:92
Console * console_get(const char *name)
Definition console.cpp:117
static const io_op_t console_io_ops
Definition console.cpp:87
std::array< Console *, 128 > console_list
Definition console.cpp:19
static size_t console_io_write(io_t *io, const void *data, size_t size)
Definition console.cpp:76
list_head consoles
Definition console.cpp:17
@ CONSOLE_CAP_COLOR
Definition console.hpp:16
@ CONSOLE_CAP_READ
console supports read
Definition console.hpp:21
bool signal_has_pending(void)
Return true if there's a pending signal.
Definition signal.cpp:296
long signal_send_to_thread(Thread *target, signal_t signal)
Send a signal to a thread.
Definition signal.cpp:87
MOSAPI s32 strncmp(const char *str1, const char *str2, size_t n)
MOSAPI s32 strcmp(const char *str1, const char *str2)
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition list.cpp:68
#define list_foreach(t, v, h)
Iterate over a list.
Definition list.hpp:89
#define list_node(element)
Get the ‘list_node’ of a list element. This is exactly the reverse of ‘list_entry’ above.
Definition list.hpp:74
list_node_t list_head
A linked list head.
Definition list.hpp:23
MOSAPI bool list_is_empty(const list_node_t *head)
Definition list.cpp:21
MOSAPI size_t ring_buffer_pos_pop_front(u8 *buffer, ring_buffer_pos_t *pos, u8 *buf, size_t size)
should_inline bool ring_buffer_pos_is_empty(ring_buffer_pos_t *pos)
MOSAPI void ring_buffer_pos_init(ring_buffer_pos_t *pos, size_t capacity)
should_inline size_t ring_buffer_pos_push_back_byte(u8 *buffer, ring_buffer_pos_t *pos, u8 data)
void io_init(io_t *io, io_type_t type, io_flags_t flags, const io_op_t *ops)
Definition io.cpp:45
io_flags_t
Definition io.hpp:25
@ IO_READABLE
Definition io.hpp:27
@ IO_WRITABLE
Definition io.hpp:28
@ IO_CONSOLE
Definition io.hpp:21
#define NULL
Definition pb_syshdr.h:46
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
#define pr_emerg(fmt,...)
Definition printk.hpp:39
__nodiscard bool reschedule_for_waitlist(waitlist_t *waitlist)
Definition schedule.cpp:172
#define ERESTARTSYS
Definition signal.hpp:11
size_t size
Definition slab.cpp:34
const char * name
Definition slab.cpp:35
#define spinlock_acquire(lock)
Definition spinlock.hpp:64
#define spinlock_release(lock)
Definition spinlock.hpp:65
console_caps caps
Definition console.hpp:38
virtual size_t do_write(const char *data, size_t size)=0
virtual bool extra_setup()
Definition console.hpp:98
virtual bool set_color(standard_color_t fg, standard_color_t bg)=0
struct Console::@330233373312361211116347355025210137337367045127 reader
standard_color_t default_fg
Definition console.hpp:66
ring_buffer_pos_t pos
Definition console.hpp:55
struct Console::@320337056237367035102365240171133364340037030012 writer
io_t io
Definition console.hpp:36
spinlock_t lock
Definition console.hpp:54
size_t size
Definition console.hpp:57
const char * name
Definition console.hpp:37
void putc(u8 c)
Definition console.cpp:21
standard_color_t default_bg
Definition console.hpp:66
waitlist_t waitlist
Definition console.hpp:39
u8 * buf
Definition console.hpp:56
Definition io.hpp:37
Definition io.hpp:48
The entry in the waiters list of a process, or a thread.
Definition wait.hpp:14
Thread * thread_get(tid_t id)
Definition thread.cpp:174
unsigned char u8
Definition types.h:15
#define container_of(ptr, type, member)
Definition types.hpp:31
void waitlist_init(waitlist_t *list)
Definition wait.cpp:15
size_t waitlist_wake(waitlist_t *list, size_t max_wakeups)
Definition wait.cpp:37