[29] | 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> |
---|
[39] | 25 | #include <hal_internal.h> |
---|
[29] | 26 | |
---|
| 27 | #include <memcpy.h> |
---|
| 28 | #include <thread.h> |
---|
| 29 | #include <string.h> |
---|
| 30 | #include <process.h> |
---|
| 31 | #include <printk.h> |
---|
| 32 | #include <vmm.h> |
---|
| 33 | #include <core.h> |
---|
| 34 | #include <cluster.h> |
---|
| 35 | |
---|
| 36 | #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) |
---|
| 37 | #define CONS_X_SIZE 160 |
---|
| 38 | #define CONS_Y_SIZE 80 |
---|
| 39 | |
---|
| 40 | extern intptr_t iom_base; |
---|
| 41 | size_t cons_ptr __in_kdata = 0; |
---|
| 42 | |
---|
[39] | 43 | void x86_panic(char *msg) |
---|
| 44 | { |
---|
| 45 | x86_printf("!!!!! PANIC !!!!!\n"); |
---|
| 46 | x86_printf("-> %s\n", msg); |
---|
| 47 | x86_printf("!!!!!!!!!!!!!!!!!\n"); |
---|
| 48 | while (1); |
---|
| 49 | } |
---|
| 50 | |
---|
[29] | 51 | static void x86_putc(char c) |
---|
| 52 | { |
---|
| 53 | if (c == '\n') { |
---|
| 54 | cons_ptr = roundup(cons_ptr, CONS_X_SIZE); |
---|
| 55 | return; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | char *video = (char *)iom_base + (0xB8000 - IOM_BEGIN) + cons_ptr; |
---|
| 59 | *video = c; |
---|
| 60 | cons_ptr++, video++; |
---|
| 61 | *video = 0x7; |
---|
| 62 | cons_ptr++, video++; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | static void x86_itoa(char *buf, unsigned long int n, int base) |
---|
| 66 | { |
---|
| 67 | unsigned long int tmp; |
---|
| 68 | int i, j; |
---|
| 69 | |
---|
| 70 | tmp = n; |
---|
| 71 | i = 0; |
---|
| 72 | |
---|
| 73 | do { |
---|
| 74 | tmp = n % base; |
---|
| 75 | buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10); |
---|
| 76 | } while (n /= base); |
---|
| 77 | buf[i--] = 0; |
---|
| 78 | |
---|
| 79 | for (j = 0; j < i; j++, i--) { |
---|
| 80 | tmp = buf[j]; |
---|
| 81 | buf[j] = buf[i]; |
---|
| 82 | buf[i] = tmp; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
[32] | 86 | static void x86_ztoa(char *buf, uint64_t n, uint64_t base) |
---|
| 87 | { |
---|
| 88 | uint64_t tmp; |
---|
| 89 | int i, j; |
---|
| 90 | |
---|
| 91 | tmp = n; |
---|
| 92 | i = 0; |
---|
| 93 | |
---|
| 94 | do { |
---|
| 95 | tmp = n % base; |
---|
| 96 | buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10); |
---|
| 97 | } while (n /= base); |
---|
| 98 | buf[i--] = 0; |
---|
| 99 | |
---|
| 100 | for (j = 0; j < i; j++, i--) { |
---|
| 101 | tmp = buf[j]; |
---|
| 102 | buf[j] = buf[i]; |
---|
| 103 | buf[i] = tmp; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
[29] | 107 | void x86_printf(char *s, ...) |
---|
| 108 | { |
---|
| 109 | va_list ap; |
---|
| 110 | |
---|
| 111 | char buf[16]; |
---|
| 112 | int i, j, size, buflen, neg; |
---|
| 113 | |
---|
| 114 | unsigned char c; |
---|
| 115 | int ival; |
---|
| 116 | unsigned int uival; |
---|
[32] | 117 | uint64_t zval; |
---|
[29] | 118 | |
---|
| 119 | va_start(ap, s); |
---|
| 120 | |
---|
| 121 | while ((c = *s++)) { |
---|
| 122 | size = 0; |
---|
| 123 | neg = 0; |
---|
| 124 | |
---|
| 125 | if (c == 0) |
---|
| 126 | break; |
---|
| 127 | else if (c == '%') { |
---|
| 128 | c = *s++; |
---|
| 129 | if (c >= '0' && c <= '9') { |
---|
| 130 | size = c - '0'; |
---|
| 131 | c = *s++; |
---|
| 132 | } |
---|
| 133 | |
---|
[32] | 134 | if (c == 'z') { |
---|
| 135 | zval = va_arg(ap, uint64_t); |
---|
| 136 | x86_ztoa(buf, zval, 10); |
---|
| 137 | |
---|
| 138 | buflen = strlen(buf); |
---|
| 139 | if (buflen < size) |
---|
| 140 | for (i = size, j = buflen; i >= 0; i--, j--) |
---|
| 141 | buf[i] = (j >= 0) ? buf[j] : '0'; |
---|
| 142 | |
---|
| 143 | x86_printf(buf); |
---|
| 144 | } else if (c == 'Z') { |
---|
| 145 | zval = va_arg(ap, uint64_t); |
---|
| 146 | x86_ztoa(buf, zval, 16); |
---|
| 147 | |
---|
| 148 | buflen = strlen(buf); |
---|
| 149 | if (buflen < size) |
---|
| 150 | for (i = size, j = buflen; i >= 0; i--, j--) |
---|
| 151 | buf[i] = (j >= 0) ? buf[j] : '0'; |
---|
| 152 | |
---|
| 153 | x86_printf("0x%s", buf); |
---|
| 154 | } else if (c == 'd') { |
---|
[29] | 155 | ival = va_arg(ap, int); |
---|
| 156 | if (ival < 0) { |
---|
| 157 | uival = 0 - ival; |
---|
| 158 | neg++; |
---|
| 159 | } else |
---|
| 160 | uival = ival; |
---|
| 161 | x86_itoa(buf, uival, 10); |
---|
| 162 | |
---|
| 163 | buflen = strlen(buf); |
---|
| 164 | if (buflen < size) |
---|
| 165 | for (i = size, j = buflen; i >= 0; |
---|
| 166 | i--, j--) |
---|
| 167 | buf[i] = |
---|
| 168 | (j >= |
---|
| 169 | 0) ? buf[j] : '0'; |
---|
| 170 | |
---|
| 171 | if (neg) |
---|
| 172 | x86_printf("-%s", buf); |
---|
| 173 | else |
---|
| 174 | x86_printf(buf); |
---|
| 175 | } else if (c == 'u') { |
---|
| 176 | uival = va_arg(ap, int); |
---|
| 177 | x86_itoa(buf, uival, 10); |
---|
| 178 | |
---|
| 179 | buflen = strlen(buf); |
---|
| 180 | if (buflen < size) |
---|
| 181 | for (i = size, j = buflen; i >= 0; |
---|
| 182 | i--, j--) |
---|
| 183 | buf[i] = |
---|
| 184 | (j >= |
---|
| 185 | 0) ? buf[j] : '0'; |
---|
| 186 | |
---|
| 187 | x86_printf(buf); |
---|
| 188 | } else if (c == 'x' || c == 'X') { |
---|
| 189 | uival = va_arg(ap, int); |
---|
| 190 | x86_itoa(buf, uival, 16); |
---|
| 191 | |
---|
| 192 | buflen = strlen(buf); |
---|
| 193 | if (buflen < size) |
---|
| 194 | for (i = size, j = buflen; i >= 0; |
---|
| 195 | i--, j--) |
---|
| 196 | buf[i] = |
---|
| 197 | (j >= |
---|
| 198 | 0) ? buf[j] : '0'; |
---|
| 199 | |
---|
| 200 | x86_printf("0x%s", buf); |
---|
| 201 | } else if (c == 'p') { |
---|
| 202 | uival = va_arg(ap, int); |
---|
| 203 | x86_itoa(buf, uival, 16); |
---|
| 204 | size = 8; |
---|
| 205 | |
---|
| 206 | buflen = strlen(buf); |
---|
| 207 | if (buflen < size) |
---|
| 208 | for (i = size, j = buflen; i >= 0; |
---|
| 209 | i--, j--) |
---|
| 210 | buf[i] = |
---|
| 211 | (j >= |
---|
| 212 | 0) ? buf[j] : '0'; |
---|
| 213 | |
---|
| 214 | x86_printf("0x%s", buf); |
---|
| 215 | } else if (c == 's') { |
---|
| 216 | x86_printf((char *) va_arg(ap, int)); |
---|
| 217 | } |
---|
| 218 | } else |
---|
| 219 | x86_putc(c); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | return; |
---|
| 223 | } |
---|