blob: e146c9731bd9efa69df12cfb09bb8a0c59e8342f [file] [log] [blame]
Kevin O'Connor9521e262008-07-04 13:04:29 -04001// Misc utility functions.
2//
Kevin O'Connor5b199ac2009-05-06 23:23:01 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor9521e262008-07-04 13:04:29 -04004//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor9521e262008-07-04 13:04:29 -04006
Kevin O'Connor273e8ae2009-01-19 19:56:07 -05007#include "util.h" // call16
Kevin O'Connor9521e262008-07-04 13:04:29 -04008#include "bregs.h" // struct bregs
Kevin O'Connor7cefbfa2009-12-10 21:35:49 -05009#include "config.h" // BUILD_STACK_ADDR
Kevin O'Connora5826b52009-10-24 17:57:29 -040010
Kevin O'Connor10ad7992009-10-24 11:06:08 -040011
12/****************************************************************
13 * 16bit calls
14 ****************************************************************/
15
Kevin O'Connor9521e262008-07-04 13:04:29 -040016// Call a function with a specified register state. Note that on
17// return, the interrupt enable/disable flag may be altered.
18inline void
19call16(struct bregs *callregs)
20{
Kevin O'Connor52a300f2009-12-26 23:32:57 -050021 if (!MODESEGMENT && getesp() > BUILD_STACK_ADDR)
Kevin O'Connora5826b52009-10-24 17:57:29 -040022 panic("call16 with invalid stack\n");
Kevin O'Connor9521e262008-07-04 13:04:29 -040023 asm volatile(
Kevin O'Connor7ab798f2008-07-13 11:08:36 -040024#if MODE16 == 1
Kevin O'Connor9521e262008-07-04 13:04:29 -040025 "calll __call16\n"
Kevin O'Connorf094ba82009-04-13 19:30:27 -040026 "cli\n"
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040027 "cld"
Kevin O'Connor9521e262008-07-04 13:04:29 -040028#else
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040029 "calll __call16_from32"
Kevin O'Connor9521e262008-07-04 13:04:29 -040030#endif
31 : "+a" (callregs), "+m" (*callregs)
32 :
Kevin O'Connor9caf7862009-02-27 20:14:05 -050033 : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
Kevin O'Connor9521e262008-07-04 13:04:29 -040034}
35
36inline void
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050037call16big(struct bregs *callregs)
38{
Kevin O'Connor52a300f2009-12-26 23:32:57 -050039 ASSERT32FLAT();
Kevin O'Connora5826b52009-10-24 17:57:29 -040040 if (getesp() > BUILD_STACK_ADDR)
41 panic("call16 with invalid stack\n");
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050042 asm volatile(
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040043 "calll __call16big_from32"
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050044 : "+a" (callregs), "+m" (*callregs)
45 :
Kevin O'Connor9caf7862009-02-27 20:14:05 -050046 : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050047}
48
49inline void
Kevin O'Connor9521e262008-07-04 13:04:29 -040050__call16_int(struct bregs *callregs, u16 offset)
51{
Kevin O'Connor52a300f2009-12-26 23:32:57 -050052 if (MODESEGMENT)
Kevin O'Connor9f985422009-09-09 11:34:39 -040053 callregs->code.seg = GET_SEG(CS);
Kevin O'Connor273e8ae2009-01-19 19:56:07 -050054 else
Kevin O'Connor9f985422009-09-09 11:34:39 -040055 callregs->code.seg = SEG_BIOS;
56 callregs->code.offset = offset;
Kevin O'Connor9521e262008-07-04 13:04:29 -040057 call16(callregs);
58}
Kevin O'Connora4d35762008-03-08 15:43:03 -050059
Kevin O'Connor10ad7992009-10-24 11:06:08 -040060// 16bit trampoline for enabling irqs from 32bit mode.
61ASM16(
Kevin O'Connora5826b52009-10-24 17:57:29 -040062 " .global trampoline_checkirqs\n"
63 "trampoline_checkirqs:\n"
Kevin O'Connor10ad7992009-10-24 11:06:08 -040064 " rep ; nop\n"
65 " lretw"
66 );
67
Kevin O'Connore9a67bf2009-12-09 20:04:54 -050068void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050069check_irqs(void)
Kevin O'Connor10ad7992009-10-24 11:06:08 -040070{
71 if (MODE16) {
Kevin O'Connor7cefbfa2009-12-10 21:35:49 -050072 asm volatile(
73 "sti\n"
74 "nop\n"
75 "rep ; nop\n"
76 "cli\n"
77 "cld\n"
78 : : :"memory");
79 } else {
80 extern void trampoline_checkirqs();
81 struct bregs br;
82 br.flags = F_IF;
83 br.code.seg = SEG_BIOS;
84 br.code.offset = (u32)&trampoline_checkirqs;
85 call16big(&br);
Kevin O'Connor10ad7992009-10-24 11:06:08 -040086 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -040087}
88
89
90/****************************************************************
91 * String ops
92 ****************************************************************/
93
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040094// Sum the bytes in the specified area.
95u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050096checksum_far(u16 buf_seg, void *buf_far, u32 len)
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040097{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050098 SET_SEG(ES, buf_seg);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040099 u32 i;
100 u8 sum = 0;
101 for (i=0; i<len; i++)
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500102 sum += GET_VAR(ES, ((u8*)buf_far)[i]);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -0400103 return sum;
104}
105
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500106u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500107checksum(void *buf, u32 len)
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500108{
109 return checksum_far(GET_SEG(SS), buf, len);
110}
111
Kevin O'Connor67823442009-04-13 14:14:51 -0400112size_t
113strlen(const char *s)
114{
115 if (__builtin_constant_p(s))
116 return __builtin_strlen(s);
117 const char *p = s;
118 while (*p)
119 p++;
120 return p-s;
121}
122
123// Compare two areas of memory.
124int
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400125memcmp(const void *s1, const void *s2, size_t n)
Kevin O'Connor67823442009-04-13 14:14:51 -0400126{
127 while (n) {
128 if (*(u8*)s1 != *(u8*)s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400129 return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
Kevin O'Connor67823442009-04-13 14:14:51 -0400130 s1++;
131 s2++;
132 n--;
133 }
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400134 return 0;
Kevin O'Connor67823442009-04-13 14:14:51 -0400135}
136
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400137// Compare two strings.
138int
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400139strcmp(const char *s1, const char *s2)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400140{
141 for (;;) {
142 if (*s1 != *s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400143 return *s1 < *s2 ? -1 : 1;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400144 if (! *s1)
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400145 return 0;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400146 s1++;
147 s2++;
148 }
149}
150
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400151inline void
152memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
153{
154 SET_SEG(ES, d_seg);
155 asm volatile(
156 "rep stosb %%es:(%%di)"
157 : "+c"(len), "+D"(d_far)
158 : "a"(c)
159 : "cc", "memory");
160}
161
162inline void
163memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
164{
165 len /= 2;
166 SET_SEG(ES, d_seg);
167 asm volatile(
168 "rep stosw %%es:(%%di)"
169 : "+c"(len), "+D"(d_far)
170 : "a"(c)
171 : "cc", "memory");
172}
173
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400174void *
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400175memset(void *s, int c, size_t n)
176{
177 while (n)
178 ((char *)s)[--n] = c;
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400179 return s;
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400180}
181
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500182inline void
183memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400184{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500185 SET_SEG(ES, d_seg);
186 u16 bkup_ds;
187 asm volatile(
188 "movw %%ds, %w0\n"
189 "movw %w4, %%ds\n"
190 "rep movsb (%%si),%%es:(%%di)\n"
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400191 "movw %w0, %%ds"
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500192 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
193 : "r"(s_seg)
194 : "cc", "memory");
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400195}
196
Kevin O'Connor942d4952009-06-10 22:44:06 -0400197void *
198#undef memcpy
199memcpy(void *d1, const void *s1, size_t len)
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500200#if MODESEGMENT == 0
Kevin O'Connor942d4952009-06-10 22:44:06 -0400201#define memcpy __builtin_memcpy
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400202#endif
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400203{
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400204 SET_SEG(ES, GET_SEG(SS));
Kevin O'Connor942d4952009-06-10 22:44:06 -0400205 void *d = d1;
206 if (((u32)d1 | (u32)s1 | len) & 3) {
207 // non-aligned memcpy
208 asm volatile(
209 "rep movsb (%%esi),%%es:(%%edi)"
210 : "+c"(len), "+S"(s1), "+D"(d)
211 : : "cc", "memory");
212 return d1;
213 }
214 // Common case - use 4-byte copy
Kevin O'Connor5d7b3f62009-04-19 20:05:50 -0400215 len /= 4;
216 asm volatile(
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400217 "rep movsl (%%esi),%%es:(%%edi)"
Kevin O'Connor942d4952009-06-10 22:44:06 -0400218 : "+c"(len), "+S"(s1), "+D"(d)
Kevin O'Connor5d7b3f62009-04-19 20:05:50 -0400219 : : "cc", "memory");
Kevin O'Connor942d4952009-06-10 22:44:06 -0400220 return d1;
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400221}
222
Kevin O'Connor34036962009-12-05 18:51:53 -0500223// Copy from memory mapped IO. IO mem is very slow, so yield
224// periodically. 'len' must be 4 byte aligned.
225void
226iomemcpy(void *d, const void *s, u32 len)
227{
228 yield();
229 while (len) {
230 u32 copylen = len;
Kevin O'Connorad901592009-12-13 11:25:25 -0500231 if (copylen > 2048)
232 copylen = 2048;
Kevin O'Connor34036962009-12-05 18:51:53 -0500233 len -= copylen;
234 copylen /= 4;
235 asm volatile(
236 "rep movsl (%%esi),%%es:(%%edi)"
237 : "+c"(copylen), "+S"(s), "+D"(d)
238 : : "cc", "memory");
239 yield();
240 }
241}
242
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400243void *
Kevin O'Connorc7812932008-06-08 23:08:12 -0400244memmove(void *d, const void *s, size_t len)
245{
246 if (s >= d)
247 return memcpy(d, s, len);
248
249 d += len-1;
250 s += len-1;
251 while (len--) {
252 *(char*)d = *(char*)s;
253 d--;
254 s--;
255 }
256
257 return d;
258}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500259
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500260// Copy a string - truncating it if necessary.
261char *
262strtcpy(char *dest, const char *src, size_t len)
263{
264 char *d = dest;
Kevin O'Connordac46b12010-02-17 22:49:01 -0500265 while (--len && *src != '\0')
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500266 *d++ = *src++;
267 *d = '\0';
268 return dest;
269}
270
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400271
272/****************************************************************
273 * Keyboard calls
274 ****************************************************************/
275
276// Wait for 'usec' microseconds using (with irqs enabled) using int 1586.
Kevin O'Connor5ca4b952009-02-15 13:45:48 -0500277void
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400278biosusleep(u32 usec)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500279{
280 struct bregs br;
281 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400282 br.flags = F_IF;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500283 br.ah = 0x86;
284 br.cx = usec >> 16;
285 br.dx = usec;
286 call16_int(0x15, &br);
287}
288
289// See if a keystroke is pending in the keyboard buffer.
290static int
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500291check_for_keystroke(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500292{
293 struct bregs br;
294 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400295 br.flags = F_IF;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500296 br.ah = 1;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400297 start_preempt();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500298 call16_int(0x16, &br);
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400299 finish_preempt();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500300 return !(br.flags & F_ZF);
301}
302
303// Return a keystroke - waiting forever if necessary.
304static int
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500305get_raw_keystroke(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500306{
307 struct bregs br;
308 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400309 br.flags = F_IF;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400310 start_preempt();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500311 call16_int(0x16, &br);
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400312 finish_preempt();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500313 return br.ah;
314}
315
316// Read a keystroke - waiting up to 'msec' milliseconds.
317int
318get_keystroke(int msec)
319{
320 for (;;) {
321 if (check_for_keystroke())
322 return get_raw_keystroke();
323 if (msec <= 0)
324 return -1;
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400325 start_preempt();
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400326 biosusleep(50*1000);
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400327 finish_preempt();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500328 msec -= 50;
329 }
330}