blob: dcc0b71b83eae3a8e369b01f12410ebaf9a0c3a2 [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'Connor9eb21002012-01-29 13:30:56 -050011void
12cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
13{
14 // Check for cpu id
15 u32 origflags = save_flags();
16 restore_flags(origflags ^ F_ID);
17 u32 newflags = save_flags();
18 restore_flags(origflags);
19
20 if (((origflags ^ newflags) & F_ID) != F_ID)
21 // no cpuid
22 *eax = *ebx = *ecx = *edx = 0;
23 else
24 __cpuid(index, eax, ebx, ecx, edx);
25}
26
Kevin O'Connor10ad7992009-10-24 11:06:08 -040027
28/****************************************************************
Kevin O'Connor10ad7992009-10-24 11:06:08 -040029 * String ops
30 ****************************************************************/
31
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040032// Sum the bytes in the specified area.
33u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050034checksum_far(u16 buf_seg, void *buf_far, u32 len)
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040035{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050036 SET_SEG(ES, buf_seg);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040037 u32 i;
38 u8 sum = 0;
39 for (i=0; i<len; i++)
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050040 sum += GET_VAR(ES, ((u8*)buf_far)[i]);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -040041 return sum;
42}
43
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050044u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050045checksum(void *buf, u32 len)
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050046{
47 return checksum_far(GET_SEG(SS), buf, len);
48}
49
Kevin O'Connor67823442009-04-13 14:14:51 -040050size_t
51strlen(const char *s)
52{
53 if (__builtin_constant_p(s))
54 return __builtin_strlen(s);
55 const char *p = s;
56 while (*p)
57 p++;
58 return p-s;
59}
60
61// Compare two areas of memory.
62int
Kevin O'Connor38d1a342009-04-18 16:59:47 -040063memcmp(const void *s1, const void *s2, size_t n)
Kevin O'Connor67823442009-04-13 14:14:51 -040064{
65 while (n) {
66 if (*(u8*)s1 != *(u8*)s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -040067 return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
Kevin O'Connor67823442009-04-13 14:14:51 -040068 s1++;
69 s2++;
70 n--;
71 }
Kevin O'Connor38d1a342009-04-18 16:59:47 -040072 return 0;
Kevin O'Connor67823442009-04-13 14:14:51 -040073}
74
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040075// Compare two strings.
76int
Kevin O'Connor38d1a342009-04-18 16:59:47 -040077strcmp(const char *s1, const char *s2)
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040078{
79 for (;;) {
80 if (*s1 != *s2)
Kevin O'Connor38d1a342009-04-18 16:59:47 -040081 return *s1 < *s2 ? -1 : 1;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040082 if (! *s1)
Kevin O'Connor38d1a342009-04-18 16:59:47 -040083 return 0;
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040084 s1++;
85 s2++;
86 }
87}
88
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040089inline void
90memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
91{
92 SET_SEG(ES, d_seg);
93 asm volatile(
94 "rep stosb %%es:(%%di)"
95 : "+c"(len), "+D"(d_far)
Kevin O'Connor41966da2012-05-28 18:57:06 -040096 : "a"(c), "m" (__segment_ES)
Kevin O'Connor5b199ac2009-05-06 23:23:01 -040097 : "cc", "memory");
98}
99
100inline void
101memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
102{
103 len /= 2;
104 SET_SEG(ES, d_seg);
105 asm volatile(
106 "rep stosw %%es:(%%di)"
107 : "+c"(len), "+D"(d_far)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400108 : "a"(c), "m" (__segment_ES)
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400109 : "cc", "memory");
110}
111
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400112void *
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400113memset(void *s, int c, size_t n)
114{
115 while (n)
116 ((char *)s)[--n] = c;
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400117 return s;
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400118}
119
Gerd Hoffmann0a806082010-11-29 09:42:11 +0100120void memset_fl(void *ptr, u8 val, size_t size)
121{
122 if (MODESEGMENT)
123 memset_far(FLATPTR_TO_SEG(ptr), (void*)(FLATPTR_TO_OFFSET(ptr)),
124 val, size);
125 else
126 memset(ptr, val, size);
127}
128
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500129inline void
130memcpy_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 -0400131{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500132 SET_SEG(ES, d_seg);
133 u16 bkup_ds;
134 asm volatile(
135 "movw %%ds, %w0\n"
136 "movw %w4, %%ds\n"
137 "rep movsb (%%si),%%es:(%%di)\n"
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400138 "movw %w0, %%ds"
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500139 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400140 : "r"(s_seg), "m" (__segment_ES)
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500141 : "cc", "memory");
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400142}
143
Kevin O'Connor8f59aa32010-06-06 16:11:45 -0400144inline void
145memcpy_fl(void *d_fl, const void *s_fl, size_t len)
146{
147 if (MODESEGMENT)
148 memcpy_far(FLATPTR_TO_SEG(d_fl), (void*)FLATPTR_TO_OFFSET(d_fl)
149 , FLATPTR_TO_SEG(s_fl), (void*)FLATPTR_TO_OFFSET(s_fl)
150 , len);
151 else
152 memcpy(d_fl, s_fl, len);
153}
154
Kevin O'Connor942d4952009-06-10 22:44:06 -0400155void *
156#undef memcpy
157memcpy(void *d1, const void *s1, size_t len)
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500158#if MODESEGMENT == 0
Kevin O'Connor942d4952009-06-10 22:44:06 -0400159#define memcpy __builtin_memcpy
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400160#endif
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400161{
Kevin O'Connor5a1d0fc2009-06-15 23:35:30 -0400162 SET_SEG(ES, GET_SEG(SS));
Kevin O'Connor942d4952009-06-10 22:44:06 -0400163 void *d = d1;
164 if (((u32)d1 | (u32)s1 | len) & 3) {
165 // non-aligned memcpy
166 asm volatile(
167 "rep movsb (%%esi),%%es:(%%edi)"
168 : "+c"(len), "+S"(s1), "+D"(d)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400169 : "m" (__segment_ES) : "cc", "memory");
Kevin O'Connor942d4952009-06-10 22:44:06 -0400170 return d1;
171 }
172 // Common case - use 4-byte copy
Kevin O'Connor5d7b3f62009-04-19 20:05:50 -0400173 len /= 4;
174 asm volatile(
Kevin O'Connor5b199ac2009-05-06 23:23:01 -0400175 "rep movsl (%%esi),%%es:(%%edi)"
Kevin O'Connor942d4952009-06-10 22:44:06 -0400176 : "+c"(len), "+S"(s1), "+D"(d)
Kevin O'Connor41966da2012-05-28 18:57:06 -0400177 : "m" (__segment_ES) : "cc", "memory");
Kevin O'Connor942d4952009-06-10 22:44:06 -0400178 return d1;
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400179}
180
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400181// Copy to/from memory mapped IO. IO mem is very slow, so yield
182// periodically.
Kevin O'Connor34036962009-12-05 18:51:53 -0500183void
184iomemcpy(void *d, const void *s, u32 len)
185{
Kevin O'Connor41966da2012-05-28 18:57:06 -0400186 ASSERT32FLAT();
Kevin O'Connor34036962009-12-05 18:51:53 -0500187 yield();
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400188 while (len > 3) {
Kevin O'Connor34036962009-12-05 18:51:53 -0500189 u32 copylen = len;
Kevin O'Connorad901592009-12-13 11:25:25 -0500190 if (copylen > 2048)
191 copylen = 2048;
Kevin O'Connor34036962009-12-05 18:51:53 -0500192 copylen /= 4;
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400193 len -= copylen * 4;
Kevin O'Connor34036962009-12-05 18:51:53 -0500194 asm volatile(
195 "rep movsl (%%esi),%%es:(%%edi)"
196 : "+c"(copylen), "+S"(s), "+D"(d)
197 : : "cc", "memory");
198 yield();
199 }
Kevin O'Connorb4525a02010-07-27 01:14:11 -0400200 if (len)
201 // Copy any remaining bytes.
202 memcpy(d, s, len);
Kevin O'Connor34036962009-12-05 18:51:53 -0500203}
204
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400205void *
Kevin O'Connorc7812932008-06-08 23:08:12 -0400206memmove(void *d, const void *s, size_t len)
207{
208 if (s >= d)
209 return memcpy(d, s, len);
210
211 d += len-1;
212 s += len-1;
213 while (len--) {
214 *(char*)d = *(char*)s;
215 d--;
216 s--;
217 }
218
219 return d;
220}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500221
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500222// Copy a string - truncating it if necessary.
223char *
224strtcpy(char *dest, const char *src, size_t len)
225{
226 char *d = dest;
Kevin O'Connordac46b12010-02-17 22:49:01 -0500227 while (--len && *src != '\0')
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500228 *d++ = *src++;
229 *d = '\0';
230 return dest;
231}
232
Kevin O'Connor2e109a62010-12-24 10:39:32 -0500233// locate first occurance of character c in the string s
234char *
235strchr(const char *s, int c)
236{
237 for (; *s; s++)
238 if (*s == c)
239 return (char*)s;
240 return NULL;
241}
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400242
Kevin O'Connor9e881a32011-01-08 12:06:54 -0500243// Remove any trailing blank characters (spaces, new lines, carriage returns)
244void
245nullTrailingSpace(char *buf)
246{
247 int len = strlen(buf);
248 char *end = &buf[len-1];
249 while (end >= buf && *end <= ' ')
250 *(end--) = '\0';
251}
252
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400253/****************************************************************
254 * Keyboard calls
255 ****************************************************************/
256
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500257// See if a keystroke is pending in the keyboard buffer.
258static int
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500259check_for_keystroke(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500260{
261 struct bregs br;
262 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400263 br.flags = F_IF;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500264 br.ah = 1;
265 call16_int(0x16, &br);
266 return !(br.flags & F_ZF);
267}
268
269// Return a keystroke - waiting forever if necessary.
270static int
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500271get_raw_keystroke(void)
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500272{
273 struct bregs br;
274 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400275 br.flags = F_IF;
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500276 call16_int(0x16, &br);
277 return br.ah;
278}
279
280// Read a keystroke - waiting up to 'msec' milliseconds.
281int
282get_keystroke(int msec)
283{
Kevin O'Connorbb685912010-05-23 12:40:40 -0400284 u32 end = calc_future_timer(msec);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500285 for (;;) {
286 if (check_for_keystroke())
287 return get_raw_keystroke();
Kevin O'Connorbb685912010-05-23 12:40:40 -0400288 if (check_timer(end))
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500289 return -1;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400290 yield_toirq();
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500291 }
292}