]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/io/console/misc.cc
Initial checkin from Perforce.
[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 conditions:
11 // 
12 //     * Redistributions of source code must retain the above copyright notice,
13 //       this list of conditions and the following disclaimers.
14 // 
15 //     * Redistributions in binary form must reproduce the above copyright notice,
16 //       this list of conditions and the following disclaimers in the
17 //       documentation and/or other materials provided with the distribution.
18 // 
19 //     * The names of the Software's authors and/or contributors
20 //       may not be used to endorse or promote products derived from
21 //       this Software without specific prior written permission.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
26 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
29 // SOFTWARE.
30
31 #include <kern/console.h>
32 #include <kern/libc.h>
33 #include <util/spinlock.h>
34
35 namespace IO {
36 namespace Console {
37         void Console::write(char *data, size_t len)
38         {
39                 u64 len64 = len;
40                 write(Array<octet>((octet *)data, len), &len64);
41         }
42         
43         // All consoles will set this to themselves; the last console
44         // to be initialized will be used.  At some point, the console
45         // used will be configurable (including the ability to broadcast
46         // to multiple consoles).
47         
48         Console *primary_console;
49 }
50 }
51
52 static const size_t printf_buffer_size = 4096;
53 static char printf_buffer[printf_buffer_size];
54 Lock::SpinLock printf_lock;
55
56 // Note: this will not retry with a larger buffer if overflow
57 // happens.  If you really need that, you'll need to call snprintf
58 // and primary_console->write yourself.
59
60 void vga_write(uint8_t *buf, u64 len);
61
62 size_t printf(const char *str, ...)
63 {
64         Lock::AutoSpinLockRecIRQ autolock(printf_lock);
65
66         va_list args;
67         va_start(args, str);
68         size_t ret = vsnprintf(printf_buffer, printf_buffer_size, str, args);
69         va_end(args);
70         
71         if (ret > printf_buffer_size)
72                 ret = printf_buffer_size;
73         
74         IO::Console::primary_console->write(printf_buffer, ret);
75         return ret;
76 }