source: trunk/hal/x86_64/x86_printf.c @ 29

Last change on this file since 29 was 29, checked in by max@…, 7 years ago

Update. The kernel now enables the GDT/IDT, and has trap entries. A
x86_printf function is added for debugging purposes only. The new Makefile
will come in another commit.

File size: 3.5 KB
Line 
1/*
2 * x86_printf.c - A printf function for x86 (debug only).
3 *
4 * Copyright (c) [don't know exactly, found on the internet... anyway, this
5 *                file will be removed soon...]
6 *
7 * This file is part of ALMOS-MKH.
8 *
9 * ALMOS-MKH is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2.0 of the License.
12 *
13 * ALMOS-MKH is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <hal_types.h>
24#include <hal_boot.h>
25
26#include <memcpy.h>
27#include <thread.h>
28#include <string.h>
29#include <process.h>
30#include <printk.h>
31#include <vmm.h>
32#include <core.h>
33#include <cluster.h>
34
35#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
36#define CONS_X_SIZE     160
37#define CONS_Y_SIZE     80
38
39extern intptr_t iom_base;
40size_t cons_ptr __in_kdata = 0;
41
42static void x86_putc(char c)
43{
44        size_t i;
45
46        if (c == '\n') {
47                cons_ptr = roundup(cons_ptr, CONS_X_SIZE);
48                return;
49        }
50
51        char *video = (char *)iom_base + (0xB8000 - IOM_BEGIN) + cons_ptr;
52        *video = c;
53        cons_ptr++, video++;
54        *video = 0x7;
55        cons_ptr++, video++;
56}
57
58static void x86_itoa(char *buf, unsigned long int n, int base)
59{
60        unsigned long int tmp;
61        int i, j;
62
63        tmp = n;
64        i = 0;
65
66        do {
67                tmp = n % base;
68                buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10);
69        } while (n /= base);
70        buf[i--] = 0;
71
72        for (j = 0; j < i; j++, i--) {
73                tmp = buf[j];
74                buf[j] = buf[i];
75                buf[i] = tmp;
76        }
77}
78
79void x86_printf(char *s, ...)
80{
81        va_list ap;
82
83        char buf[16];
84        int i, j, size, buflen, neg;
85
86        unsigned char c;
87        int ival;
88        unsigned int uival;
89
90        va_start(ap, s);
91
92        while ((c = *s++)) {
93                size = 0;
94                neg = 0;
95
96                if (c == 0)
97                        break;
98                else if (c == '%') {
99                        c = *s++;
100                        if (c >= '0' && c <= '9') {
101                                size = c - '0';
102                                c = *s++;
103                        }
104
105                        if (c == 'd') {
106                                ival = va_arg(ap, int);
107                                if (ival < 0) {
108                                        uival = 0 - ival;
109                                        neg++;
110                                } else
111                                        uival = ival;
112                                x86_itoa(buf, uival, 10);
113
114                                buflen = strlen(buf);
115                                if (buflen < size)
116                                        for (i = size, j = buflen; i >= 0;
117                                             i--, j--)
118                                                buf[i] =
119                                                    (j >=
120                                                     0) ? buf[j] : '0';
121
122                                if (neg)
123                                        x86_printf("-%s", buf);
124                                else
125                                        x86_printf(buf);
126                        } else if (c == 'u') {
127                                uival = va_arg(ap, int);
128                                x86_itoa(buf, uival, 10);
129
130                                buflen = strlen(buf);
131                                if (buflen < size)
132                                        for (i = size, j = buflen; i >= 0;
133                                             i--, j--)
134                                                buf[i] =
135                                                    (j >=
136                                                     0) ? buf[j] : '0';
137
138                                x86_printf(buf);
139                        } else if (c == 'x' || c == 'X') {
140                                uival = va_arg(ap, int);
141                                x86_itoa(buf, uival, 16);
142
143                                buflen = strlen(buf);
144                                if (buflen < size)
145                                        for (i = size, j = buflen; i >= 0;
146                                             i--, j--)
147                                                buf[i] =
148                                                    (j >=
149                                                     0) ? buf[j] : '0';
150
151                                x86_printf("0x%s", buf);
152                        } else if (c == 'p') {
153                                uival = va_arg(ap, int);
154                                x86_itoa(buf, uival, 16);
155                                size = 8;
156
157                                buflen = strlen(buf);
158                                if (buflen < size)
159                                        for (i = size, j = buflen; i >= 0;
160                                             i--, j--)
161                                                buf[i] =
162                                                    (j >=
163                                                     0) ? buf[j] : '0';
164
165                                x86_printf("0x%s", buf);
166                        } else if (c == 's') {
167                                x86_printf((char *) va_arg(ap, int));
168                        } 
169                } else
170                        x86_putc(c);
171        }
172
173        return;
174}
Note: See TracBrowser for help on using the repository browser.