]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/io/console/vga.cc
Switch to a simple X11-style license.
[polintos/scott/priv.git] / kernel / io / console / vga.cc
1 // io/console/vga.cc -- Kernel debugging console for standard VGA text mode
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 <System/IO.h>
24 #include <kern/kernel.h>
25 #include <kern/console.h>
26 #include <kern/notifier.h>
27 #include <kern/io.h>
28 #include <kern/libc.h>
29 #include <kern/addrs.h>
30 #include <util/lock.h>
31
32 void vga_write(char *str);
33
34 namespace IO {
35 namespace Console {
36         class VGA : public Console {
37                 Lock::SpinLock lock;
38         
39                 u16 *mem_base;
40                 unsigned long io_base;
41                 
42                 int line_width;
43                 int lines;
44                 int pos;
45                 
46                 void putc(int ch, int attr)
47                 {
48                         if (ch == '\n') {
49                                 pos += line_width;
50                                 pos -= pos % line_width;
51                         } else if (ch == '\r') {
52                                 pos -= pos % line_width;
53                         } else {
54                                 // Use ll_swap_le16 rather than store_le16 in the hopes
55                                 // that the compiler can simply change the shifts around
56                                 // rather than assembling it one way and then ll_swapping.
57                                 //
58                                 // It's done as one atomic store rather than two single
59                                 // byte writes (which would eliminate endianness concerns)
60                                 // to eliminate the possibility of a race where the video
61                                 // card reads a character before its attribute has been
62                                 // written.
63                                 
64                                 mem_base[pos++] = ll_swap_le16(ch | (attr << 8));
65                         }
66                         
67                         if (pos >= lines * line_width) {
68                                 pos -= line_width;
69                                 
70                                 // FIXME: until memcpy is more than a stupid byte-at-a-time
71                                 // loop, this could race with the video card.
72                                 
73                                 memcpy(mem_base, mem_base + line_width,
74                                        line_width * (lines - 1) * sizeof(u16));
75                                 
76                                 for (int i = 0; i < line_width; i++)
77                                         mem_base[pos + i] = ll_swap_le16(' ' | (7 << 8));
78                         }
79                 }
80                 
81                 void get_cursor()
82                 {
83                         ll_out_8(0x3d4, 14);
84                         pos = (u16)ll_in_8(0x3d5) << 8;
85                         ll_out_8(0x3d4, 15);
86                         pos |= ll_in_8(0x3d5);
87                 }
88
89                 void set_cursor()
90                 {
91                         ll_out_8(0x3d4, 14);
92                         ll_out_8(0x3d5, pos >> 8);
93                         ll_out_8(0x3d4, 15);
94                         ll_out_8(0x3d5, pos & 0xff);
95                 }
96                 
97         public:
98                 #include <servers/io/console/vga/IO/Console/VGA.h>
99                 
100                 VGA()
101                 {
102                         init_iface();
103
104                         // FIXME: should be PCI mem space, not phys mem.
105                         mem_base = (u16 *)Arch::phys_to_kvirt(0xb8000);
106                         io_base = 0x3d0;
107                         
108                         // FIXME: detect
109                         line_width = 80;
110                         lines = 25;
111
112                         primary_console = this;
113                 }
114                 
115                 void write(Array<octet> buf, u64 *LEN)
116                 {
117                         if (!valid_size_t(*LEN))
118                                 /* throw something */
119                                 return;
120                         
121                         size_t len = *LEN;
122                         
123                         if (len > buf.count)
124                                 /* throw something */
125                                 return;
126                 
127                         Lock::AutoSpinLockRecIRQ autolock(lock);
128                         get_cursor();
129                         
130                         for (size_t i = 0; i < len; i++)
131                                 putc(buf.ptr[i], 7);
132
133                         set_cursor();                   
134                 }
135                 
136                 void write_async(Array<octet> buf, u64 len, Notifier notifier)
137                 {
138                         write(buf, &len);
139                         io_notify(notifier, len, io_result::Success);
140                 }
141         };
142         
143         // Use a static constructor, so that it can be used before
144         // memory management is up and running.
145         
146         VGA vga;
147 }
148 }
149
150 #include <servers/io/console/vga/footer.cc>