]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/io/console/vga.cc
fixes/cleanup
[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 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
8 // 
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
14
15 #include <System/IO.h>
16 #include <kern/kernel.h>
17 #include <kern/console.h>
18 #include <kern/notifier.h>
19 #include <kern/io.h>
20 #include <kern/libc.h>
21 #include <kern/addrs.h>
22 #include <util/lock.h>
23
24 namespace IO {
25 namespace Console {
26         class VGA : public Console {
27                 Lock::SpinLock lock;
28         
29                 u16 *mem_base;
30                 unsigned long io_base;
31                 
32                 int line_width;
33                 int lines;
34                 int pos;
35                 
36                 void putc(int ch, int attr)
37                 {
38                         if (ch == '\n') {
39                                 pos += line_width;
40                                 pos -= pos % line_width;
41                         } else if (ch == '\r') {
42                                 pos -= pos % line_width;
43                         } else {
44                                 // Use ll_swap_le16 rather than store_le16 in the hopes
45                                 // that the compiler can simply change the shifts around
46                                 // rather than assembling it one way and then ll_swapping.
47                                 //
48                                 // It's done as one atomic store rather than two single
49                                 // byte writes (which would eliminate endianness concerns)
50                                 // to eliminate the possibility of a race where the video
51                                 // card reads a character before its attribute has been
52                                 // written.
53                                 
54                                 mem_base[pos++] = ll_swap_le16(ch | (attr << 8));
55                         }
56                         
57                         if (pos >= lines * line_width) {
58                                 pos -= line_width;
59                                 
60                                 // FIXME: until memcpy is more than a stupid byte-at-a-time
61                                 // loop, this could race with the video card.
62                                 
63                                 memcpy(mem_base, mem_base + line_width,
64                                        line_width * (lines - 1) * sizeof(u16));
65                                 
66                                 for (int i = 0; i < line_width; i++)
67                                         mem_base[pos + i] = ll_swap_le16(' ' | (7 << 8));
68                         }
69                 }
70                 
71                 void get_cursor()
72                 {
73                         ll_out_8(0x3d4, 14);
74                         pos = (u16)ll_in_8(0x3d5) << 8;
75                         ll_out_8(0x3d4, 15);
76                         pos |= ll_in_8(0x3d5);
77                 }
78
79                 void set_cursor()
80                 {
81                         ll_out_8(0x3d4, 14);
82                         ll_out_8(0x3d5, pos >> 8);
83                         ll_out_8(0x3d4, 15);
84                         ll_out_8(0x3d5, pos & 0xff);
85                 }
86                 
87         public:
88                 #include <servers/io/console/vga/IO/Console/VGA.h>
89                 
90                 VGA()
91                 {
92                         init_iface();
93
94                         // FIXME: should be PCI mem space, not phys mem.
95                         mem_base = (u16 *)Arch::phys_to_kvirt(0xb8000);
96                         io_base = 0x3d0;
97                         
98                         // FIXME: detect
99                         line_width = 80;
100                         lines = 25;
101
102                         primary_console = this;
103                 }
104                 
105                 void write(Array<octet> buf, u64 *LEN)
106                 {
107                         if (!valid_size_t(*LEN))
108                                 /* throw something */
109                                 return;
110                         
111                         size_t len = *LEN;
112                         
113                         if (len > buf.count)
114                                 /* throw something */
115                                 return;
116                 
117                         Lock::AutoSpinLockRecIRQ autolock(lock);
118                         get_cursor();
119                         
120                         for (size_t i = 0; i < len; i++)
121                                 putc(buf.ptr[i], 7);
122
123                         set_cursor();                   
124                 }
125                 
126                 void write_async(Array<octet> buf, u64 len, Notifier notifier)
127                 {
128                         write(buf, &len);
129                         io_notify(notifier, len, io_result::Success);
130                 }
131         };
132         
133         // Use a static constructor, so that it can be used before
134         // memory management is up and running.
135         
136         VGA vga;
137 }
138 }
139
140 #include <servers/io/console/vga/footer.cc>