blob: 579a044e52626ac1b3583c65f3391827ab242c00 [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
61/****************************************************************
62 * String ops
63 ****************************************************************/
64
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040065// Sum the bytes in the specified area.
66u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050067checksum_far(u16 buf_seg, void *buf_far, u32 len)
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040068{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050069 SET_SEG(ES, buf_seg);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040070 u32 i;
71 u8 sum = 0;
72 for (i=0; i<len; i++)
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050073 sum += GET_VAR(ES, ((u8*)buf_far)[i]);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040074 return sum;
75}
76
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050077u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050078checksum(void *buf, u32 len)
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050079{
80 return checksum_far(GET_SEG(SS), buf, len);
81}
82
Kevin O'Connor67823442009-04-13 14:14:51 -040083size_t
84strlen(const char *s)
85{
86 if (__builtin_constant_p(s))
87 return __builtin_strlen(s);
88 const char *p = s;
89 while (*p)
90 p++;
91 return p-s;
92}
93
94// Compare two areas of memory.
95int
Kevin O'Connor38d1a342009-04-18 16:59:47 -040096memcmp(const void *s1, const void *s2, size_t n)
Kevin O'Connor67823442009-04-13 14:14:51 -040097{
98 while (n) {
99 if (*(u8*)s1 != *(u8*)s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400100 return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
Kevin O'Connor67823442009-04-13 14:14:51 -0400101 s1++;
102 s2++;
103 n--;
104 }
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400105 return 0;
Kevin O'Connor67823442009-04-13 14:14:51 -0400106}
107
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400108// Compare two strings.
109int
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400110strcmp(const char *s1, const char *s2)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400111{
112 for (;;) {
113 if (*s1 != *s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400114 return *s1 < *s2 ? -1 : 1;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400115 if (! *s1)
Kevin O'Connor38d1a342009-04-18 16:59:47 -0400116 return 0;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400117 s1++;
118 s2++;
119 }
120}
121
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400122inline void
123memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
124{
125 SET_SEG(ES, d_seg);
126 asm volatile(
127 "rep stosb %%es:(%%di)"
128 : "+c"(len), "+D"(d_far)
129 : "a"(c)
130 : "cc", "memory");
131}
132
133inline void
134memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
135{
136 len /= 2;
137 SET_SEG(ES, d_seg);
138 asm volatile(
139 "rep stosw %%es:(%%di)"
140 : "+c"(len), "+D"(d_far)
141 : "a"(c)
142 : "cc", "memory");
143}
144
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400145void *
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400146memset(void *s, int c, size_t n)
147{
148 while (n)
149 ((char *)s)[--n] = c;
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400150 return s;
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400151}
152
Gerd Hoffmann0a806082010-11-29 09:42:11 +0100153void memset_fl(void *ptr, u8 val, size_t size)
154{
155 if (MODESEGMENT)
156 memset_far(FLATPTR_TO_SEG(ptr), (void*)(FLATPTR_TO_OFFSET(ptr)),
157 val, size);
158 else
159 memset(ptr, val, size);
160}
161
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500162inline void
163memcpy_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 -0400164{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500165 SET_SEG(ES, d_seg);
166 u16 bkup_ds;
167 asm volatile(
168 "movw %%ds, %w0\n"
169 "movw %w4, %%ds\n"
170 "rep movsb (%%si),%%es:(%%di)\n"
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400171 "movw %w0, %%ds"
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500172 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
173 : "r"(s_seg)
174 : "cc", "memory");
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400175}
176
Kevin O'Connor8f59aa32010-06-06 16:11:45 -0400177inline void
178memcpy_fl(void *d_fl, const void *s_fl, size_t len)
179{
180 if (MODESEGMENT)
181 memcpy_far(FLATPTR_TO_SEG(d_fl), (void*)FLATPTR_TO_OFFSET(d_fl)
182 , FLATPTR_TO_SEG(s_fl), (void*)FLATPTR_TO_OFFSET(s_fl)
183 , len);
184 else
185 memcpy(d_fl, s_fl, len);
186}
187
Kevin O'Connor942d4952009-06-10 22:44:06 -0400188void *
189#undef memcpy
190memcpy(void *d1, const void *s1, size_t len)
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500191#if MODESEGMENT == 0
Kevin O'Connor942d4952009-06-10 22:44:06 -0400192#define memcpy __builtin_memcpy
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400193#endif
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400194{
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400195 SET_SEG(ES, GET_SEG(SS));
Kevin O'Connor942d4952009-06-10 22:44:06 -0400196 void *d = d1;
197 if (((u32)d1 | (u32)s1 | len) & 3) {
198 // non-aligned memcpy
199 asm volatile(
200 "rep movsb (%%esi),%%es:(%%edi)"
201 : "+c"(len), "+S"(s1), "+D"(d)
202 : : "cc", "memory");
203 return d1;
204 }
205 // Common case - use 4-byte copy
Kevin O'Connor5d7b3f62009-04-19 20:05:50 -0400206 len /= 4;
207 asm volatile(
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400208 "rep movsl (%%esi),%%es:(%%edi)"
Kevin O'Connor942d4952009-06-10 22:44:06 -0400209 : "+c"(len), "+S"(s1), "+D"(d)
Kevin O'Connor5d7b3f62009-04-19 20:05:50 -0400210 : : "cc", "memory");
Kevin O'Connor942d4952009-06-10 22:44:06 -0400211 return d1;
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400212}
213
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400214// Copy to/from memory mapped IO. IO mem is very slow, so yield
215// periodically.
Kevin O'Connor34036962009-12-05 18:51:53 -0500216void
217iomemcpy(void *d, const void *s, u32 len)
218{
219 yield();
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400220 while (len > 3) {
Kevin O'Connor34036962009-12-05 18:51:53 -0500221 u32 copylen = len;
Kevin O'Connorad901592009-12-13 11:25:25 -0500222 if (copylen > 2048)
223 copylen = 2048;
Kevin O'Connor34036962009-12-05 18:51:53 -0500224 copylen /= 4;
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400225 len -= copylen * 4;
Kevin O'Connor34036962009-12-05 18:51:53 -0500226 asm volatile(
227 "rep movsl (%%esi),%%es:(%%edi)"
228 : "+c"(copylen), "+S"(s), "+D"(d)
229 : : "cc", "memory");
230 yield();
231 }
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400232 if (len)
233 // Copy any remaining bytes.
234 memcpy(d, s, len);
Kevin O'Connor34036962009-12-05 18:51:53 -0500235}
236
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400237void *
Kevin O'Connorc7812932008-06-08 23:08:12 -0400238memmove(void *d, const void *s, size_t len)
239{
240 if (s >= d)
241 return memcpy(d, s, len);
242
243 d += len-1;
244 s += len-1;
245 while (len--) {
246 *(char*)d = *(char*)s;
247 d--;
248 s--;
249 }
250
251 return d;
252}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500253
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500254// Copy a string - truncating it if necessary.
255char *
256strtcpy(char *dest, const char *src, size_t len)
257{
258 char *d = dest;
Kevin O'Connordac46b12010-02-17 22:49:01 -0500259 while (--len && *src != '\0')
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500260 *d++ = *src++;
261 *d = '\0';
262 return dest;
263}
264
Kevin O'Connor2e109a62010-12-24 10:39:32 -0500265// locate first occurance of character c in the string s
266char *
267strchr(const char *s, int c)
268{
269 for (; *s; s++)
270 if (*s == c)
271 return (char*)s;
272 return NULL;
273}
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400274
275/****************************************************************
276 * Keyboard calls
277 ****************************************************************/
278
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500279// See if a keystroke is pending in the keyboard buffer.
280static int
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500281check_for_keystroke(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500282{
283 struct bregs br;
284 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400285 br.flags = F_IF;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500286 br.ah = 1;
287 call16_int(0x16, &br);
288 return !(br.flags & F_ZF);
289}
290
291// Return a keystroke - waiting forever if necessary.
292static int
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500293get_raw_keystroke(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500294{
295 struct bregs br;
296 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400297 br.flags = F_IF;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500298 call16_int(0x16, &br);
299 return br.ah;
300}
301
302// Read a keystroke - waiting up to 'msec' milliseconds.
303int
304get_keystroke(int msec)
305{
Kevin O'Connorbb685912010-05-23 12:40:40 -0400306 u32 end = calc_future_timer(msec);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500307 for (;;) {
308 if (check_for_keystroke())
309 return get_raw_keystroke();
Kevin O'Connorbb685912010-05-23 12:40:40 -0400310 if (check_timer(end))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500311 return -1;
Kevin O'Connorbb685912010-05-23 12:40:40 -0400312 wait_irq();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500313 }
314}