blob: ae00d6c8970e5a8d837a82abcd23e78eef906684 [file] [log] [blame]
Kevin O'Connor9521e262008-07-04 13:04:29 -04001// Misc utility functions.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4//
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'Connor35ae7262009-01-19 15:44:44 -05009#include "farptr.h" // GET_FLATPTR
Kevin O'Connor7f343092009-01-01 18:31:11 -050010#include "biosvar.h" // get_ebda_seg
Kevin O'Connor9521e262008-07-04 13:04:29 -040011
12// Call a function with a specified register state. Note that on
13// return, the interrupt enable/disable flag may be altered.
14inline void
15call16(struct bregs *callregs)
16{
17 asm volatile(
Kevin O'Connor7ab798f2008-07-13 11:08:36 -040018#if MODE16 == 1
Kevin O'Connor9521e262008-07-04 13:04:29 -040019 "calll __call16\n"
20#else
21 "calll __call16_from32\n"
22#endif
23 : "+a" (callregs), "+m" (*callregs)
24 :
Kevin O'Connor9caf7862009-02-27 20:14:05 -050025 : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
Kevin O'Connor9521e262008-07-04 13:04:29 -040026}
27
28inline void
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050029call16big(struct bregs *callregs)
30{
Kevin O'Connor7f343092009-01-01 18:31:11 -050031 extern void __force_link_error__call16big_only_in_32bit_mode();
32 if (MODE16)
33 __force_link_error__call16big_only_in_32bit_mode();
34
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050035 asm volatile(
36 "calll __call16big_from32\n"
37 : "+a" (callregs), "+m" (*callregs)
38 :
Kevin O'Connor9caf7862009-02-27 20:14:05 -050039 : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050040}
41
42inline void
Kevin O'Connor9521e262008-07-04 13:04:29 -040043__call16_int(struct bregs *callregs, u16 offset)
44{
Kevin O'Connor273e8ae2009-01-19 19:56:07 -050045 if (MODE16)
46 callregs->cs = GET_SEG(CS);
47 else
48 callregs->cs = SEG_BIOS;
Kevin O'Connor9521e262008-07-04 13:04:29 -040049 callregs->ip = offset;
50 call16(callregs);
51}
Kevin O'Connora4d35762008-03-08 15:43:03 -050052
Kevin O'Connora83ff552009-01-01 21:00:59 -050053inline void
54call16_simpint(int nr, u32 *eax, u32 *flags)
55{
56 extern void __force_link_error__call16_simpint_only_in_16bit_mode();
57 if (!MODE16)
58 __force_link_error__call16_simpint_only_in_16bit_mode();
59
60 asm volatile(
61 "stc\n"
62 "int %2\n"
63 "pushfl\n"
64 "popl %1\n"
Kevin O'Connora83ff552009-01-01 21:00:59 -050065 "cli\n"
Kevin O'Connor4ebc0b72009-03-01 12:31:57 -050066 "cld\n"
Kevin O'Connora83ff552009-01-01 21:00:59 -050067 : "+a"(*eax), "=r"(*flags)
68 : "i"(nr)
69 : "cc", "memory");
70}
71
Kevin O'Connor7f343092009-01-01 18:31:11 -050072// Switch to the extra stack in ebda and call a function.
73inline u32
74stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
75{
76 extern void __force_link_error__stack_hop_only_in_16bit_mode();
77 if (!MODE16)
78 __force_link_error__stack_hop_only_in_16bit_mode();
79
Kevin O'Connorfaab1b32009-01-19 13:29:36 -050080 u16 ebda_seg = get_ebda_seg(), bkup_ss;
81 u32 bkup_esp;
Kevin O'Connor7f343092009-01-01 18:31:11 -050082 asm volatile(
Kevin O'Connorfaab1b32009-01-19 13:29:36 -050083 // Backup current %ss/%esp values.
84 "movw %%ss, %w3\n"
85 "movl %%esp, %4\n"
86 // Copy ebda seg to %ds/%ss and set %esp
87 "movw %w6, %%ds\n"
88 "movw %w6, %%ss\n"
Kevin O'Connor7f343092009-01-01 18:31:11 -050089 "movl %5, %%esp\n"
90 // Call func
Kevin O'Connorfaab1b32009-01-19 13:29:36 -050091 "calll %7\n"
Kevin O'Connor7f343092009-01-01 18:31:11 -050092 // Restore segments and stack
Kevin O'Connorfaab1b32009-01-19 13:29:36 -050093 "movw %w3, %%ds\n"
94 "movw %w3, %%ss\n"
95 "movl %4, %%esp\n"
96 : "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
97 : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
Kevin O'Connor7f343092009-01-01 18:31:11 -050098 : "cc", "memory");
99 return eax;
100}
101
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -0400102// Sum the bytes in the specified area.
103u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500104checksum_far(u16 buf_seg, void *buf_far, u32 len)
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -0400105{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500106 SET_SEG(ES, buf_seg);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -0400107 u32 i;
108 u8 sum = 0;
109 for (i=0; i<len; i++)
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500110 sum += GET_VAR(ES, ((u8*)buf_far)[i]);
Kevin O'Connor2e7ab8b2008-03-29 14:29:35 -0400111 return sum;
112}
113
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500114u8
Kevin O'Connor94fd47e2009-02-15 15:21:10 -0500115checksum(void *buf, u32 len)
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500116{
117 return checksum_far(GET_SEG(SS), buf, len);
118}
119
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400120void *
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400121memset(void *s, int c, size_t n)
122{
123 while (n)
124 ((char *)s)[--n] = c;
Kevin O'Connor5e4235f2008-04-12 09:00:04 -0400125 return s;
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400126}
127
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500128inline void
129memcpy_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 -0400130{
Kevin O'Connor8b267cb2009-01-19 19:25:21 -0500131 SET_SEG(ES, d_seg);
132 u16 bkup_ds;
133 asm volatile(
134 "movw %%ds, %w0\n"
135 "movw %w4, %%ds\n"
136 "rep movsb (%%si),%%es:(%%di)\n"
137 "movw %w0, %%ds\n"
138 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
139 : "r"(s_seg)
140 : "cc", "memory");
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400141}
142
Kevin O'Connorc7812932008-06-08 23:08:12 -0400143void *
Kevin O'Connor18b927e2008-08-29 21:14:36 -0400144memcpy(void *d1, const void *s1, size_t len)
145{
146 u8 *d = (u8*)d1, *s = (u8*)s1;
147 while (len--)
148 *d++ = *s++;
149 return d1;
150}
151
152void *
Kevin O'Connorc7812932008-06-08 23:08:12 -0400153memmove(void *d, const void *s, size_t len)
154{
155 if (s >= d)
156 return memcpy(d, s, len);
157
158 d += len-1;
159 s += len-1;
160 while (len--) {
161 *(char*)d = *(char*)s;
162 d--;
163 s--;
164 }
165
166 return d;
167}
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500168
Kevin O'Connor71f036d2009-02-08 16:57:22 -0500169// Copy a string - truncating it if necessary.
170char *
171strtcpy(char *dest, const char *src, size_t len)
172{
173 char *d = dest;
174 while (len-- && *src != '\0')
175 *d++ = *src++;
176 *d = '\0';
177 return dest;
178}
179
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500180// Wait for 'usec' microseconds with irqs enabled.
Kevin O'Connor5ca4b952009-02-15 13:45:48 -0500181void
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500182usleep(u32 usec)
183{
184 struct bregs br;
185 memset(&br, 0, sizeof(br));
186 br.ah = 0x86;
187 br.cx = usec >> 16;
188 br.dx = usec;
189 call16_int(0x15, &br);
190}
191
192// See if a keystroke is pending in the keyboard buffer.
193static int
194check_for_keystroke()
195{
196 struct bregs br;
197 memset(&br, 0, sizeof(br));
198 br.ah = 1;
199 call16_int(0x16, &br);
200 return !(br.flags & F_ZF);
201}
202
203// Return a keystroke - waiting forever if necessary.
204static int
205get_raw_keystroke()
206{
207 struct bregs br;
208 memset(&br, 0, sizeof(br));
209 call16_int(0x16, &br);
210 return br.ah;
211}
212
213// Read a keystroke - waiting up to 'msec' milliseconds.
214int
215get_keystroke(int msec)
216{
217 for (;;) {
218 if (check_for_keystroke())
219 return get_raw_keystroke();
220 if (msec <= 0)
221 return -1;
222 usleep(50*1000);
223 msec -= 50;
224 }
225}