Replace common segment/offset pairs with struct segoff_s.
[seabios.git] / src / util.c
1 // Misc utility functions.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "util.h" // call16
8 #include "bregs.h" // struct bregs
9 #include "farptr.h" // GET_FLATPTR
10 #include "biosvar.h" // get_ebda_seg
11
12 // Call a function with a specified register state.  Note that on
13 // return, the interrupt enable/disable flag may be altered.
14 inline void
15 call16(struct bregs *callregs)
16 {
17     asm volatile(
18 #if MODE16 == 1
19         "calll __call16\n"
20         "cli\n"
21         "cld"
22 #else
23         "calll __call16_from32"
24 #endif
25         : "+a" (callregs), "+m" (*callregs)
26         :
27         : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
28 }
29
30 inline void
31 call16big(struct bregs *callregs)
32 {
33     ASSERT32();
34     asm volatile(
35         "calll __call16big_from32"
36         : "+a" (callregs), "+m" (*callregs)
37         :
38         : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
39 }
40
41 inline void
42 __call16_int(struct bregs *callregs, u16 offset)
43 {
44     if (MODE16)
45         callregs->code.seg = GET_SEG(CS);
46     else
47         callregs->code.seg = SEG_BIOS;
48     callregs->code.offset = offset;
49     call16(callregs);
50 }
51
52 // Switch to the extra stack in ebda and call a function.
53 inline u32
54 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
55 {
56     ASSERT16();
57     u16 ebda_seg = get_ebda_seg(), bkup_ss;
58     u32 bkup_esp;
59     asm volatile(
60         // Backup current %ss/%esp values.
61         "movw %%ss, %w3\n"
62         "movl %%esp, %4\n"
63         // Copy ebda seg to %ds/%ss and set %esp
64         "movw %w6, %%ds\n"
65         "movw %w6, %%ss\n"
66         "movl %5, %%esp\n"
67         // Call func
68         "calll %7\n"
69         // Restore segments and stack
70         "movw %w3, %%ds\n"
71         "movw %w3, %%ss\n"
72         "movl %4, %%esp"
73         : "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
74         : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
75         : "cc", "memory");
76     return eax;
77 }
78
79 // Sum the bytes in the specified area.
80 u8
81 checksum_far(u16 buf_seg, void *buf_far, u32 len)
82 {
83     SET_SEG(ES, buf_seg);
84     u32 i;
85     u8 sum = 0;
86     for (i=0; i<len; i++)
87         sum += GET_VAR(ES, ((u8*)buf_far)[i]);
88     return sum;
89 }
90
91 u8
92 checksum(void *buf, u32 len)
93 {
94     return checksum_far(GET_SEG(SS), buf, len);
95 }
96
97 size_t
98 strlen(const char *s)
99 {
100     if (__builtin_constant_p(s))
101         return __builtin_strlen(s);
102     const char *p = s;
103     while (*p)
104         p++;
105     return p-s;
106 }
107
108 // Compare two areas of memory.
109 int
110 memcmp(const void *s1, const void *s2, size_t n)
111 {
112     while (n) {
113         if (*(u8*)s1 != *(u8*)s2)
114             return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
115         s1++;
116         s2++;
117         n--;
118     }
119     return 0;
120 }
121
122 // Compare two strings.
123 int
124 strcmp(const char *s1, const char *s2)
125 {
126     for (;;) {
127         if (*s1 != *s2)
128             return *s1 < *s2 ? -1 : 1;
129         if (! *s1)
130             return 0;
131         s1++;
132         s2++;
133     }
134 }
135
136 inline void
137 memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
138 {
139     SET_SEG(ES, d_seg);
140     asm volatile(
141         "rep stosb %%es:(%%di)"
142         : "+c"(len), "+D"(d_far)
143         : "a"(c)
144         : "cc", "memory");
145 }
146
147 inline void
148 memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
149 {
150     len /= 2;
151     SET_SEG(ES, d_seg);
152     asm volatile(
153         "rep stosw %%es:(%%di)"
154         : "+c"(len), "+D"(d_far)
155         : "a"(c)
156         : "cc", "memory");
157 }
158
159 void *
160 memset(void *s, int c, size_t n)
161 {
162     while (n)
163         ((char *)s)[--n] = c;
164     return s;
165 }
166
167 inline void
168 memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
169 {
170     SET_SEG(ES, d_seg);
171     u16 bkup_ds;
172     asm volatile(
173         "movw %%ds, %w0\n"
174         "movw %w4, %%ds\n"
175         "rep movsb (%%si),%%es:(%%di)\n"
176         "movw %w0, %%ds"
177         : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
178         : "r"(s_seg)
179         : "cc", "memory");
180 }
181
182 void *
183 #undef memcpy
184 memcpy(void *d1, const void *s1, size_t len)
185 #if MODE16 == 0
186 #define memcpy __builtin_memcpy
187 #endif
188 {
189     SET_SEG(ES, GET_SEG(SS));
190     void *d = d1;
191     if (((u32)d1 | (u32)s1 | len) & 3) {
192         // non-aligned memcpy
193         asm volatile(
194             "rep movsb (%%esi),%%es:(%%edi)"
195             : "+c"(len), "+S"(s1), "+D"(d)
196             : : "cc", "memory");
197         return d1;
198     }
199     // Common case - use 4-byte copy
200     len /= 4;
201     asm volatile(
202         "rep movsl (%%esi),%%es:(%%edi)"
203         : "+c"(len), "+S"(s1), "+D"(d)
204         : : "cc", "memory");
205     return d1;
206 }
207
208 void *
209 memmove(void *d, const void *s, size_t len)
210 {
211     if (s >= d)
212         return memcpy(d, s, len);
213
214     d += len-1;
215     s += len-1;
216     while (len--) {
217         *(char*)d = *(char*)s;
218         d--;
219         s--;
220     }
221
222     return d;
223 }
224
225 // Copy a string - truncating it if necessary.
226 char *
227 strtcpy(char *dest, const char *src, size_t len)
228 {
229     char *d = dest;
230     while (len-- && *src != '\0')
231         *d++ = *src++;
232     *d = '\0';
233     return dest;
234 }
235
236 // Wait for 'usec' microseconds with irqs enabled.
237 void
238 usleep(u32 usec)
239 {
240     struct bregs br;
241     memset(&br, 0, sizeof(br));
242     br.ah = 0x86;
243     br.cx = usec >> 16;
244     br.dx = usec;
245     call16_int(0x15, &br);
246 }
247
248 // See if a keystroke is pending in the keyboard buffer.
249 static int
250 check_for_keystroke()
251 {
252     struct bregs br;
253     memset(&br, 0, sizeof(br));
254     br.ah = 1;
255     call16_int(0x16, &br);
256     return !(br.flags & F_ZF);
257 }
258
259 // Return a keystroke - waiting forever if necessary.
260 static int
261 get_raw_keystroke()
262 {
263     struct bregs br;
264     memset(&br, 0, sizeof(br));
265     call16_int(0x16, &br);
266     return br.ah;
267 }
268
269 // Read a keystroke - waiting up to 'msec' milliseconds.
270 int
271 get_keystroke(int msec)
272 {
273     for (;;) {
274         if (check_for_keystroke())
275             return get_raw_keystroke();
276         if (msec <= 0)
277             return -1;
278         usleep(50*1000);
279         msec -= 50;
280     }
281 }