]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/io/console/misc.cc
b66e4ae57634f94be1bc3c24a81e241c3b1c0148
[polintos/scott/priv.git] / kernel / io / console / misc.cc
1 // io/console/misc.cc -- Generic code relating to kernel debug consoles
2 //
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
4 // 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following condition:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
21 // SOFTWARE.
22
23 #include <kern/console.h>
24 #include <kern/libc.h>
25 #include <util/spinlock.h>
26
27 namespace IO {
28 namespace Console {
29         void Console::write(char *data, size_t len)
30         {
31                 u64 len64 = len;
32                 write(Array<octet>((octet *)data, len), &len64);
33         }
34         
35         // All consoles will set this to themselves; the last console
36         // to be initialized will be used.  At some point, the console
37         // used will be configurable (including the ability to broadcast
38         // to multiple consoles).
39         
40         Console *primary_console;
41 }
42 }
43
44 static const size_t printf_buffer_size = 4096;
45 static char printf_buffer[printf_buffer_size];
46 Lock::SpinLock printf_lock;
47
48 // Note: this will not retry with a larger buffer if overflow
49 // happens.  If you really need that, you'll need to call snprintf
50 // and primary_console->write yourself.
51
52 void vga_write(uint8_t *buf, u64 len);
53
54 size_t printf(const char *str, ...)
55 {
56         Lock::AutoSpinLockRecIRQ autolock(printf_lock);
57
58         va_list args;
59         va_start(args, str);
60         size_t ret = vsnprintf(printf_buffer, printf_buffer_size, str, args);
61         va_end(args);
62         
63         if (ret > printf_buffer_size)
64                 ret = printf_buffer_size;
65         
66         IO::Console::primary_console->write(printf_buffer, ret);
67         return ret;
68 }