Statistics
| Branch: | Revision:

root / darwin-user / commpage.c @ 81a97d9d

History | View | Annotate | Download (12.8 kB)

1
 /*
2
 *  Commpage syscalls
3
 *
4
 *  Copyright (c) 2006 Pierre d'Herbemont
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <fcntl.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <errno.h>
23

    
24
#include <mach/message.h>
25
#include <mach/mach.h>
26
#include <mach/mach_time.h>
27
#include <sys/time.h>
28
#include <sys/mman.h>
29
#include <libkern/OSAtomic.h>
30

    
31
#include "qemu.h"
32

    
33
//#define DEBUG_COMMPAGE
34

    
35
#ifdef DEBUG_COMMPAGE
36
# define DPRINTF(...) do { qemu_log(__VA_ARGS__); printf(__VA_ARGS__); } while(0)
37
#else
38
# define DPRINTF(...) do { qemu_log(__VA_ARGS__); } while(0)
39
#endif
40

    
41
/********************************************************************
42
 *   Commpage definitions
43
 */
44
#ifdef TARGET_I386
45
/* Reserve space for the commpage see xnu/osfmk/i386/cpu_capabilities.h */
46
# define COMMPAGE_START (-16 * 4096) /* base address is -20 * 4096 */
47
# define COMMPAGE_SIZE  (0x1240) /* _COMM_PAGE_AREA_LENGTH is 19 * 4096 */
48
#elif defined(TARGET_PPC)
49
/* Reserve space for the commpage see xnu/osfmk/ppc/cpu_capabilities.h */
50
# define COMMPAGE_START (-8*4096)
51
# define COMMPAGE_SIZE  (2*4096) /* its _COMM_PAGE_AREA_USED but _COMM_PAGE_AREA_LENGTH is 7*4096 */
52
#endif
53

    
54
void do_compare_and_swap32(void *cpu_env, int num);
55
void do_compare_and_swap64(void *cpu_env, int num);
56
void do_add_atomic_word32(void *cpu_env, int num);
57
void do_cgettimeofday(void *cpu_env, int num, uint32_t arg1);
58
void do_nanotime(void *cpu_env, int num);
59

    
60
void unimpl_commpage(void *cpu_env, int num);
61

    
62
typedef void (*commpage_8args_function_t)(uint32_t arg1, uint32_t arg2, uint32_t arg3,
63
                uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
64
                uint32_t arg8);
65
typedef void (*commpage_indirect_function_t)(void *cpu_env, int num, uint32_t arg1,
66
                uint32_t arg2, uint32_t arg3,  uint32_t arg4, uint32_t arg5,
67
                uint32_t arg6, uint32_t arg7, uint32_t arg8);
68

    
69
#define HAS_PTR  0x10
70
#define NO_PTR   0x20
71
#define CALL_DIRECT   0x1
72
#define CALL_INDIRECT 0x2
73

    
74
#define COMMPAGE_ENTRY(name, nargs, offset, func, options) \
75
    { #name, offset, nargs, options, (commpage_8args_function_t)func }
76

    
77
struct commpage_entry {
78
    char * name;
79
    int offset;
80
    int nargs;
81
    char options;
82
    commpage_8args_function_t function;
83
};
84

    
85
static inline int commpage_code_num(struct commpage_entry *entry)
86
{
87
    if((entry->options & HAS_PTR))
88
        return entry->offset + 4;
89
    else
90
        return entry->offset;
91
}
92

    
93
static inline int commpage_is_indirect(struct commpage_entry *entry)
94
{
95
    return !(entry->options & CALL_DIRECT);
96
}
97

    
98
/********************************************************************
99
 *   Commpage entry
100
 */
101
static struct commpage_entry commpage_entries[] =
102
{
103
    COMMPAGE_ENTRY(compare_and_swap32,    0, 0x080,  do_compare_and_swap32, CALL_INDIRECT | HAS_PTR),
104
    COMMPAGE_ENTRY(compare_and_swap64,    0, 0x0c0,  do_compare_and_swap64, CALL_INDIRECT | HAS_PTR),
105
    COMMPAGE_ENTRY(enqueue,               0, 0x100,  unimpl_commpage,       CALL_INDIRECT),
106
    COMMPAGE_ENTRY(dequeue,               0, 0x140,  unimpl_commpage,       CALL_INDIRECT),
107
    COMMPAGE_ENTRY(memory_barrier,        0, 0x180,  unimpl_commpage,       CALL_INDIRECT),
108
    COMMPAGE_ENTRY(add_atomic_word32,     0, 0x1a0,  do_add_atomic_word32,  CALL_INDIRECT | HAS_PTR),
109
    COMMPAGE_ENTRY(add_atomic_word64,     0, 0x1c0,  unimpl_commpage,       CALL_INDIRECT | HAS_PTR),
110

    
111
    COMMPAGE_ENTRY(mach_absolute_time,    0, 0x200,  unimpl_commpage,       CALL_INDIRECT),
112
    COMMPAGE_ENTRY(spinlock_try,          1, 0x220,  unimpl_commpage,       CALL_INDIRECT),
113
    COMMPAGE_ENTRY(spinlock_lock,         1, 0x260,  OSSpinLockLock,        CALL_DIRECT),
114
    COMMPAGE_ENTRY(spinlock_unlock,       1, 0x2a0,  OSSpinLockUnlock,      CALL_DIRECT),
115
    COMMPAGE_ENTRY(pthread_getspecific,   0, 0x2c0,  unimpl_commpage,       CALL_INDIRECT),
116
    COMMPAGE_ENTRY(gettimeofday,          1, 0x2e0,  do_cgettimeofday,      CALL_INDIRECT),
117
    COMMPAGE_ENTRY(sys_dcache_flush,      0, 0x4e0,  unimpl_commpage,       CALL_INDIRECT),
118
    COMMPAGE_ENTRY(sys_icache_invalidate, 0, 0x520,  unimpl_commpage,       CALL_INDIRECT),
119
    COMMPAGE_ENTRY(pthread_self,          0, 0x580,  unimpl_commpage,       CALL_INDIRECT),
120

    
121
    COMMPAGE_ENTRY(relinquish,            0, 0x5c0,  unimpl_commpage,       CALL_INDIRECT),
122

    
123
#ifdef TARGET_I386
124
    COMMPAGE_ENTRY(bts,                   0, 0x5e0,  unimpl_commpage,       CALL_INDIRECT),
125
    COMMPAGE_ENTRY(btc,                   0, 0x5f0,  unimpl_commpage,       CALL_INDIRECT),
126
#endif
127

    
128
    COMMPAGE_ENTRY(bzero,                 2, 0x600,  bzero,                 CALL_DIRECT),
129
    COMMPAGE_ENTRY(bcopy,                 3, 0x780,  bcopy,                 CALL_DIRECT),
130
    COMMPAGE_ENTRY(memcpy,                3, 0x7a0,  memcpy,                CALL_DIRECT),
131

    
132
#ifdef TARGET_I386
133
    COMMPAGE_ENTRY(old_nanotime,          0, 0xf80,  do_nanotime,           CALL_INDIRECT),
134
    COMMPAGE_ENTRY(memset_pattern,        0, 0xf80,  unimpl_commpage,       CALL_INDIRECT),
135
    COMMPAGE_ENTRY(long_copy,             0, 0x1200, unimpl_commpage,       CALL_INDIRECT),
136

    
137
    COMMPAGE_ENTRY(sysintegrity,          0, 0x1600, unimpl_commpage,       CALL_INDIRECT),
138

    
139
    COMMPAGE_ENTRY(nanotime,              0, 0x1700, do_nanotime,           CALL_INDIRECT),
140
#elif TARGET_PPC
141
    COMMPAGE_ENTRY(compare_and_swap32b,   0, 0xf80,  unimpl_commpage,       CALL_INDIRECT),
142
    COMMPAGE_ENTRY(compare_and_swap64b,   0, 0xfc0,  unimpl_commpage,       CALL_INDIRECT),
143
    COMMPAGE_ENTRY(memset_pattern,        0, 0x1000, unimpl_commpage,       CALL_INDIRECT),
144
    COMMPAGE_ENTRY(bigcopy,               0, 0x1140, unimpl_commpage,       CALL_INDIRECT),
145
#endif
146
};
147

    
148

    
149
/********************************************************************
150
 *   Commpage backdoor
151
 */
152
static inline void print_commpage_entry(struct commpage_entry entry)
153
{
154
    printf("@0x%x %s\n", entry.offset, entry.name);
155
}
156

    
157
static inline void install_commpage_backdoor_for_entry(struct commpage_entry entry)
158
{
159
#ifdef TARGET_I386
160
    char * commpage = (char*)(COMMPAGE_START+entry.offset);
161
    int c = 0;
162
    if(entry.options & HAS_PTR)
163
    {
164
        commpage[c++] = (COMMPAGE_START+entry.offset+4) & 0xff;
165
        commpage[c++] = ((COMMPAGE_START+entry.offset+4) >> 8) & 0xff;
166
        commpage[c++] = ((COMMPAGE_START+entry.offset+4) >> 16) & 0xff;
167
        commpage[c++] = ((COMMPAGE_START+entry.offset+4) >> 24) & 0xff;
168
    }
169
    commpage[c++] = 0xcd;
170
    commpage[c++] = 0x79; /* int 0x79 */
171
    commpage[c++] = 0xc3; /* ret */
172
#else
173
    qerror("can't install the commpage on this arch\n");
174
#endif
175
}
176

    
177
/********************************************************************
178
 *   Commpage initialization
179
 */
180
void commpage_init(void)
181
{
182
#if (defined(__i386__) ^ defined(TARGET_I386)) || (defined(_ARCH_PPC) ^ defined(TARGET_PPC))
183
    int i;
184
    void * commpage = (void *)target_mmap( COMMPAGE_START, COMMPAGE_SIZE,
185
                           PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_FIXED, -1, 0);
186
    if((int)commpage != COMMPAGE_START)
187
        qerror("can't allocate the commpage\n");
188

    
189
    bzero(commpage, COMMPAGE_SIZE);
190

    
191
    /* XXX: commpage data not handled */
192

    
193
    for(i = 0; i < ARRAY_SIZE(commpage_entries); i++)
194
        install_commpage_backdoor_for_entry(commpage_entries[i]);
195
#else
196
    /* simply map our pages so they can be executed
197
       XXX: we don't really want to do that since in the ppc on ppc situation we may
198
       not able to run commpages host optimized instructions (like G5's on a G5),
199
       hence this is sometimes a broken fix. */
200
    page_set_flags(COMMPAGE_START, COMMPAGE_START+COMMPAGE_SIZE, PROT_EXEC | PROT_READ | PAGE_VALID);
201
#endif
202
}
203

    
204
/********************************************************************
205
 *   Commpage implementation
206
 */
207
void do_compare_and_swap32(void *cpu_env, int num)
208
{
209
#ifdef TARGET_I386
210
    uint32_t old = ((CPUX86State*)cpu_env)->regs[R_EAX];
211
    uint32_t *value = (uint32_t*)((CPUX86State*)cpu_env)->regs[R_ECX];
212
    DPRINTF("commpage: compare_and_swap32(%x,new,%p)\n", old, value);
213

    
214
    if(value && old == tswap32(*value))
215
    {
216
        uint32_t new = ((CPUX86State*)cpu_env)->regs[R_EDX];
217
        *value = tswap32(new);
218
        /* set zf flag */
219
        ((CPUX86State*)cpu_env)->eflags |= 0x40;
220
    }
221
    else
222
    {
223
        ((CPUX86State*)cpu_env)->regs[R_EAX] = tswap32(*value);
224
        /* unset zf flag */
225
        ((CPUX86State*)cpu_env)->eflags &= ~0x40;
226
    }
227
#else
228
    qerror("do_compare_and_swap32 unimplemented");
229
#endif
230
}
231

    
232
void do_compare_and_swap64(void *cpu_env, int num)
233
{
234
#ifdef TARGET_I386
235
    /* OSAtomicCompareAndSwap64 is not available on non 64 bits ppc, here is a raw implementation */
236
    uint64_t old, new, swapped_val;
237
    uint64_t *value = (uint64_t*)((CPUX86State*)cpu_env)->regs[R_ESI];
238
    old = (uint64_t)((uint64_t)((CPUX86State*)cpu_env)->regs[R_EDX]) << 32 | (uint64_t)((CPUX86State*)cpu_env)->regs[R_EAX];
239

    
240
    DPRINTF("commpage: compare_and_swap64(%" PRIx64 ",new,%p)\n", old, value);
241
    swapped_val = tswap64(*value);
242

    
243
    if(old == swapped_val)
244
    {
245
        new = (uint64_t)((uint64_t)((CPUX86State*)cpu_env)->regs[R_ECX]) << 32 | (uint64_t)((CPUX86State*)cpu_env)->regs[R_EBX];
246
        *value = tswap64(new);
247
        /* set zf flag */
248
        ((CPUX86State*)cpu_env)->eflags |= 0x40;
249
    }
250
    else
251
    {
252
        ((CPUX86State*)cpu_env)->regs[R_EAX] = (uint32_t)(swapped_val);
253
        ((CPUX86State*)cpu_env)->regs[R_EDX] = (uint32_t)(swapped_val >> 32);
254
        /* unset zf flag */
255
        ((CPUX86State*)cpu_env)->eflags &= ~0x40;
256
    }
257
#else
258
    qerror("do_compare_and_swap64 unimplemented");
259
#endif
260
}
261

    
262
void do_add_atomic_word32(void *cpu_env, int num)
263
{
264
#ifdef TARGET_I386
265
    uint32_t amt = ((CPUX86State*)cpu_env)->regs[R_EAX];
266
    uint32_t *value = (uint32_t*)((CPUX86State*)cpu_env)->regs[R_EDX];
267
    uint32_t swapped_value = tswap32(*value);
268

    
269
    DPRINTF("commpage: add_atomic_word32(%x,%p)\n", amt, value);
270

    
271
    /* old value in EAX */
272
    ((CPUX86State*)cpu_env)->regs[R_EAX] = swapped_value;
273
    *value = tswap32(swapped_value + amt);
274
#else
275
    qerror("do_add_atomic_word32 unimplemented");
276
#endif
277
}
278

    
279
void do_cgettimeofday(void *cpu_env, int num, uint32_t arg1)
280
{
281
#ifdef TARGET_I386
282
    extern int __commpage_gettimeofday(struct timeval *);
283
    DPRINTF("commpage: gettimeofday(0x%x)\n", arg1);
284
    struct timeval *time = (struct timeval *)arg1;
285
    int ret = __commpage_gettimeofday(time);
286
    tswap32s((uint32_t*)&time->tv_sec);
287
    tswap32s((uint32_t*)&time->tv_usec);
288
    ((CPUX86State*)cpu_env)->regs[R_EAX] = ret; /* Success */
289
#else
290
    qerror("do_gettimeofday unimplemented");
291
#endif
292
}
293

    
294
void do_nanotime(void *cpu_env, int num)
295
{
296
#ifdef TARGET_I386
297
    uint64_t t = mach_absolute_time();
298
    ((CPUX86State*)cpu_env)->regs[R_EAX] = (int)(t & 0xffffffff);
299
    ((CPUX86State*)cpu_env)->regs[R_EDX] = (int)((t >> 32) & 0xffffffff);
300
#else
301
    qerror("do_nanotime unimplemented");
302
#endif
303
}
304

    
305
void unimpl_commpage(void *cpu_env, int num)
306
{
307
    qerror("qemu: commpage function 0x%x not implemented\n", num);
308
}
309

    
310
/********************************************************************
311
 *   do_commpage - called by the main cpu loop
312
 */
313
void
314
do_commpage(void *cpu_env, int num, uint32_t arg1, uint32_t arg2, uint32_t arg3,
315
                uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
316
                uint32_t arg8)
317
{
318
    int i, found = 0;
319

    
320
    arg1 = tswap32(arg1);
321
    arg2 = tswap32(arg2);
322
    arg3 = tswap32(arg3);
323
    arg4 = tswap32(arg4);
324
    arg5 = tswap32(arg5);
325
    arg6 = tswap32(arg6);
326
    arg7 = tswap32(arg7);
327
    arg8 = tswap32(arg8);
328

    
329
    num = num-COMMPAGE_START-2;
330

    
331
    for(i = 0; i < ARRAY_SIZE(commpage_entries); i++) {
332
        if( num == commpage_code_num(&commpage_entries[i]) )
333
        {
334
            DPRINTF("commpage: %s %s\n", commpage_entries[i].name, commpage_is_indirect(&commpage_entries[i]) ? "[indirect]" : "[direct]");
335
            found = 1;
336
            if(commpage_is_indirect(&commpage_entries[i]))
337
            {
338
                commpage_indirect_function_t function = (commpage_indirect_function_t)commpage_entries[i].function;
339
                function(cpu_env, num, arg1, arg2, arg3,
340
                    arg4, arg5, arg6, arg7, arg8);
341
            }
342
            else
343
            {
344
                commpage_entries[i].function(arg1, arg2, arg3,
345
                    arg4, arg5, arg6, arg7, arg8);
346
            }
347
            break;
348
        }
349
    }
350

    
351
    if(!found)
352
    {
353
        gemu_log("qemu: commpage function 0x%x not defined\n", num);
354
        gdb_handlesig (cpu_env, SIGTRAP);
355
        exit(-1);
356
    }
357
}