feat: mount point flags check
[lunaix-os.git] / lunaix-os / kernel / lxconsole.c
1 #include <klibc/string.h>
2 #include <lunaix/device.h>
3 #include <lunaix/keyboard.h>
4 #include <lunaix/lxconsole.h>
5 #include <lunaix/mm/pmm.h>
6 #include <lunaix/mm/valloc.h>
7 #include <lunaix/mm/vmm.h>
8 #include <lunaix/sched.h>
9 #include <lunaix/tty/console.h>
10 #include <lunaix/tty/tty.h>
11
12 static struct console lx_console;
13
14 int
15 __tty_write(struct device* dev, void* buf, size_t offset, size_t len);
16
17 int
18 __tty_read(struct device* dev, void* buf, size_t offset, size_t len);
19
20 void
21 lxconsole_init()
22 {
23     memset(&lx_console, 0, sizeof(lx_console));
24     fifo_init(&lx_console.output, VGA_BUFFER_VADDR + 0x1000, 8192, 0);
25     fifo_init(&lx_console.input, valloc(4096), 4096, 0);
26
27     // FIXME use valloc to allocate console buffer.
28     // In doing this, the console buffer can only be accessed from kernel mode
29     //  any direct write to this buffer from user land should be purged!
30
31     // 分配控制台缓存
32     for (size_t i = 0; i < PG_ALIGN(lx_console.output.size); i += PG_SIZE) {
33         uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
34         vmm_set_mapping(PD_REFERENCED,
35                         (uintptr_t)lx_console.output.data + i,
36                         pa,
37                         PG_PREM_URW,
38                         0);
39     }
40
41     lx_console.flush_timer = NULL;
42
43     struct device* tty_dev = device_addseq(NULL, &lx_console, "tty");
44     tty_dev->write = __tty_write;
45     tty_dev->read = __tty_read;
46 }
47
48 int
49 __tty_write(struct device* dev, void* buf, size_t offset, size_t len)
50 {
51     struct console* console = (struct console*)dev->underlay;
52     console_write(console, buf, len);
53 }
54
55 int
56 __tty_read(struct device* dev, void* buf, size_t offset, size_t len)
57 {
58     struct kdb_keyinfo_pkt keyevent;
59     struct console* console = (struct console*)dev->underlay;
60
61     size_t count = fifo_read(&console->input, buf, len);
62     if (count > 0 && ((char*)buf)[count - 1] == '\n') {
63         return count;
64     }
65
66     while (count < len) {
67         // FIXME RACE!
68         //  Consider two process that is polling the input key simultaneously.
69         //  When a key is arrived, one of the processes will win the race and
70         //  swallow it (advancing the key buffer pointer)
71         if (!kbd_recv_key(&keyevent)) {
72             sched_yieldk();
73             continue;
74         }
75         if (!(keyevent.state & KBD_KEY_FPRESSED)) {
76             continue;
77         }
78         if ((keyevent.keycode & 0xff00) > KEYPAD) {
79             continue;
80         }
81
82         char c = (char)(keyevent.keycode & 0x00ff);
83         if (c == 0x08) {
84             if (fifo_backone(&console->input)) {
85                 console_write_char(c);
86             }
87             continue;
88         }
89         console_write_char(c);
90         if (!fifo_putone(&console->input, c) || c == '\n') {
91             break;
92         }
93     }
94     return count + fifo_read(&console->input, buf + count, len - count);
95 }
96
97 void
98 console_schedule_flush()
99 {
100     // TODO make the flush on-demand rather than periodic
101 }
102
103 void
104 console_view_up()
105 {
106     struct fifo_buf* buffer = &lx_console.output;
107     mutex_lock(&buffer->lock);
108     size_t p = lx_console.erd_pos - 2;
109     while (p < lx_console.erd_pos && p != buffer->wr_pos &&
110            ((char*)buffer->data)[p] != '\n') {
111         p--;
112     }
113     p++;
114
115     if (p > lx_console.erd_pos) {
116         p = 0;
117     }
118
119     buffer->flags |= FIFO_DIRTY;
120     lx_console.erd_pos = p;
121     mutex_unlock(&buffer->lock);
122 }
123
124 size_t
125 __find_next_line(size_t start)
126 {
127     size_t p = start;
128     while (p != lx_console.output.wr_pos &&
129            ((char*)lx_console.output.data)[p] != '\n') {
130         p = (p + 1) % lx_console.output.size;
131     }
132     return p + 1;
133 }
134
135 void
136 console_view_down()
137 {
138     struct fifo_buf* buffer = &lx_console.output;
139     mutex_lock(&buffer->lock);
140
141     lx_console.erd_pos = __find_next_line(lx_console.erd_pos);
142     buffer->flags |= FIFO_DIRTY;
143     mutex_unlock(&buffer->lock);
144 }
145
146 void
147 console_flush()
148 {
149     if (mutex_on_hold(&lx_console.output.lock)) {
150         return;
151     }
152     if (!(lx_console.output.flags & FIFO_DIRTY)) {
153         return;
154     }
155
156     tty_flush_buffer(lx_console.output.data,
157                      lx_console.erd_pos,
158                      lx_console.output.wr_pos,
159                      lx_console.output.size);
160     lx_console.output.flags &= ~FIFO_DIRTY;
161 }
162
163 void
164 console_write(struct console* console, uint8_t* data, size_t size)
165 {
166     mutex_lock(&console->output.lock);
167     uint8_t* buffer = console->output.data;
168     uintptr_t ptr = console->output.wr_pos;
169     uintptr_t rd_ptr = console->output.rd_pos;
170
171     char c;
172     int lines = 0;
173     int j = 0;
174     for (size_t i = 0; i < size; i++) {
175         c = data[i];
176         if (!c) {
177             continue;
178         }
179         buffer[(ptr + j) % console->output.size] = c;
180         lines += (c == '\n');
181         j++;
182     }
183
184     size = j;
185
186     uintptr_t new_ptr = (ptr + size) % console->output.size;
187     console->output.wr_pos = new_ptr;
188
189     if (console->lines > TTY_HEIGHT && lines > 0) {
190         console->output.rd_pos =
191           __find_next_line((size + rd_ptr) % console->output.size);
192     }
193
194     if (new_ptr < ptr + size && new_ptr > rd_ptr) {
195         console->output.rd_pos = new_ptr;
196     }
197
198     console->lines += lines;
199     console->erd_pos = console->output.rd_pos;
200     console->output.flags |= FIFO_DIRTY;
201     mutex_unlock(&console->output.lock);
202 }
203
204 void
205 console_write_str(char* str)
206 {
207     console_write(&lx_console, str, strlen(str));
208 }
209
210 void
211 console_write_char(char str)
212 {
213     console_write(&lx_console, &str, 1);
214 }
215
216 void
217 console_start_flushing()
218 {
219     struct lx_timer* timer =
220       timer_run_ms(20, console_flush, NULL, TIMER_MODE_PERIODIC);
221     lx_console.flush_timer = timer;
222 }