summaryrefslogtreecommitdiff
path: root/darwin-user/commpage.c
blob: 8961bef443d324358061d6ad8a57a2dbeb014ecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
 /*
 *  Commpage syscalls
 *
 *  Copyright (c) 2006 Pierre d'Herbemont
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
 *  MA 02110-1301, USA.
 */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <mach/message.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <libkern/OSAtomic.h>

#include "qemu.h"

//#define DEBUG_COMMPAGE

#ifdef DEBUG_COMMPAGE
# define DPRINTF(...) do { qemu_log(__VA_ARGS__); printf(__VA_ARGS__); } while(0)
#else
# define DPRINTF(...) do { qemu_log(__VA_ARGS__); } while(0)
#endif

/********************************************************************
 *   Commpage definitions
 */
#ifdef TARGET_I386
/* Reserve space for the commpage see xnu/osfmk/i386/cpu_capabilities.h */
# define COMMPAGE_START (-16 * 4096) /* base address is -20 * 4096 */
# define COMMPAGE_SIZE  (0x1240) /* _COMM_PAGE_AREA_LENGTH is 19 * 4096 */
#elif defined(TARGET_PPC)
/* Reserve space for the commpage see xnu/osfmk/ppc/cpu_capabilities.h */
# define COMMPAGE_START (-8*4096)
# define COMMPAGE_SIZE  (2*4096) /* its _COMM_PAGE_AREA_USED but _COMM_PAGE_AREA_LENGTH is 7*4096 */
#endif

void do_compare_and_swap32(void *cpu_env, int num);
void do_compare_and_swap64(void *cpu_env, int num);
void do_add_atomic_word32(void *cpu_env, int num);
void do_cgettimeofday(void *cpu_env, int num, uint32_t arg1);
void do_nanotime(void *cpu_env, int num);

void unimpl_commpage(void *cpu_env, int num);

typedef void (*commpage_8args_function_t)(uint32_t arg1, uint32_t arg2, uint32_t arg3,
                uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
                uint32_t arg8);
typedef void (*commpage_indirect_function_t)(void *cpu_env, int num, uint32_t arg1,
                uint32_t arg2, uint32_t arg3,  uint32_t arg4, uint32_t arg5,
                uint32_t arg6, uint32_t arg7, uint32_t arg8);

#define HAS_PTR  0x10
#define NO_PTR   0x20
#define CALL_DIRECT   0x1
#define CALL_INDIRECT 0x2

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

struct commpage_entry {
    char * name;
    int offset;
    int nargs;
    char options;
    commpage_8args_function_t function;
};

static inline int commpage_code_num(struct commpage_entry *entry)
{
    if((entry->options & HAS_PTR))
        return entry->offset + 4;
    else
        return entry->offset;
}

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

/********************************************************************
 *   Commpage entry
 */
static struct commpage_entry commpage_entries[] =
{
    COMMPAGE_ENTRY(compare_and_swap32,    0, 0x080,  do_compare_and_swap32, CALL_INDIRECT | HAS_PTR),
    COMMPAGE_ENTRY(compare_and_swap64,    0, 0x0c0,  do_compare_and_swap64, CALL_INDIRECT | HAS_PTR),
    COMMPAGE_ENTRY(enqueue,               0, 0x100,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(dequeue,               0, 0x140,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(memory_barrier,        0, 0x180,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(add_atomic_word32,     0, 0x1a0,  do_add_atomic_word32,  CALL_INDIRECT | HAS_PTR),
    COMMPAGE_ENTRY(add_atomic_word64,     0, 0x1c0,  unimpl_commpage,       CALL_INDIRECT | HAS_PTR),

    COMMPAGE_ENTRY(mach_absolute_time,    0, 0x200,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(spinlock_try,          1, 0x220,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(spinlock_lock,         1, 0x260,  OSSpinLockLock,        CALL_DIRECT),
    COMMPAGE_ENTRY(spinlock_unlock,       1, 0x2a0,  OSSpinLockUnlock,      CALL_DIRECT),
    COMMPAGE_ENTRY(pthread_getspecific,   0, 0x2c0,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(gettimeofday,          1, 0x2e0,  do_cgettimeofday,      CALL_INDIRECT),
    COMMPAGE_ENTRY(sys_dcache_flush,      0, 0x4e0,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(sys_icache_invalidate, 0, 0x520,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(pthread_self,          0, 0x580,  unimpl_commpage,       CALL_INDIRECT),

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

#ifdef TARGET_I386
    COMMPAGE_ENTRY(bts,                   0, 0x5e0,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(btc,                   0, 0x5f0,  unimpl_commpage,       CALL_INDIRECT),
#endif

    COMMPAGE_ENTRY(bzero,                 2, 0x600,  bzero,                 CALL_DIRECT),
    COMMPAGE_ENTRY(bcopy,                 3, 0x780,  bcopy,                 CALL_DIRECT),
    COMMPAGE_ENTRY(memcpy,                3, 0x7a0,  memcpy,                CALL_DIRECT),

#ifdef TARGET_I386
    COMMPAGE_ENTRY(old_nanotime,          0, 0xf80,  do_nanotime,           CALL_INDIRECT),
    COMMPAGE_ENTRY(memset_pattern,        0, 0xf80,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(long_copy,             0, 0x1200, unimpl_commpage,       CALL_INDIRECT),

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

    COMMPAGE_ENTRY(nanotime,              0, 0x1700, do_nanotime,           CALL_INDIRECT),
#elif TARGET_PPC
    COMMPAGE_ENTRY(compare_and_swap32b,   0, 0xf80,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(compare_and_swap64b,   0, 0xfc0,  unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(memset_pattern,        0, 0x1000, unimpl_commpage,       CALL_INDIRECT),
    COMMPAGE_ENTRY(bigcopy,               0, 0x1140, unimpl_commpage,       CALL_INDIRECT),
#endif
};


/********************************************************************
 *   Commpage backdoor
 */
static inline void print_commpage_entry(struct commpage_entry entry)
{
    printf("@0x%x %s\n", entry.offset, entry.name);
}

static inline void install_commpage_backdoor_for_entry(struct commpage_entry entry)
{
#ifdef TARGET_I386
    char * commpage = (char*)(COMMPAGE_START+entry.offset);
    int c = 0;
    if(entry.options & HAS_PTR)
    {
        commpage[c++] = (COMMPAGE_START+entry.offset+4) & 0xff;
        commpage[c++] = ((COMMPAGE_START+entry.offset+4) >> 8) & 0xff;
        commpage[c++] = ((COMMPAGE_START+entry.offset+4) >> 16) & 0xff;
        commpage[c++] = ((COMMPAGE_START+entry.offset+4) >> 24) & 0xff;
    }
    commpage[c++] = 0xcd;
    commpage[c++] = 0x79; /* int 0x79 */
    commpage[c++] = 0xc3; /* ret */
#else
    qerror("can't install the commpage on this arch\n");
#endif
}

/********************************************************************
 *   Commpage initialization
 */
void commpage_init(void)
{
#if (defined(__i386__) ^ defined(TARGET_I386)) || (defined(_ARCH_PPC) ^ defined(TARGET_PPC))
    int i;
    void * commpage = (void *)target_mmap( COMMPAGE_START, COMMPAGE_SIZE,
                           PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_FIXED, -1, 0);
    if((int)commpage != COMMPAGE_START)
        qerror("can't allocate the commpage\n");

    bzero(commpage, COMMPAGE_SIZE);

    /* XXX: commpage data not handled */

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

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

    if(value && old == tswap32(*value))
    {
        uint32_t new = ((CPUX86State*)cpu_env)->regs[R_EDX];
        *value = tswap32(new);
        /* set zf flag */
        ((CPUX86State*)cpu_env)->eflags |= 0x40;
    }
    else
    {
        ((CPUX86State*)cpu_env)->regs[R_EAX] = tswap32(*value);
        /* unset zf flag */
        ((CPUX86State*)cpu_env)->eflags &= ~0x40;
    }
#else
    qerror("do_compare_and_swap32 unimplemented");
#endif
}

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

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

    if(old == swapped_val)
    {
        new = (uint64_t)((uint64_t)((CPUX86State*)cpu_env)->regs[R_ECX]) << 32 | (uint64_t)((CPUX86State*)cpu_env)->regs[R_EBX];
        *value = tswap64(new);
        /* set zf flag */
        ((CPUX86State*)cpu_env)->eflags |= 0x40;
    }
    else
    {
        ((CPUX86State*)cpu_env)->regs[R_EAX] = (uint32_t)(swapped_val);
        ((CPUX86State*)cpu_env)->regs[R_EDX] = (uint32_t)(swapped_val >> 32);
        /* unset zf flag */
        ((CPUX86State*)cpu_env)->eflags &= ~0x40;
    }
#else
    qerror("do_compare_and_swap64 unimplemented");
#endif
}

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

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

    /* old value in EAX */
    ((CPUX86State*)cpu_env)->regs[R_EAX] = swapped_value;
    *value = tswap32(swapped_value + amt);
#else
    qerror("do_add_atomic_word32 unimplemented");
#endif
}

void do_cgettimeofday(void *cpu_env, int num, uint32_t arg1)
{
#ifdef TARGET_I386
    extern int __commpage_gettimeofday(struct timeval *);
    DPRINTF("commpage: gettimeofday(0x%x)\n", arg1);
    struct timeval *time = (struct timeval *)arg1;
    int ret = __commpage_gettimeofday(time);
    tswap32s((uint32_t*)&time->tv_sec);
    tswap32s((uint32_t*)&time->tv_usec);
    ((CPUX86State*)cpu_env)->regs[R_EAX] = ret; /* Success */
#else
    qerror("do_gettimeofday unimplemented");
#endif
}

void do_nanotime(void *cpu_env, int num)
{
#ifdef TARGET_I386
    uint64_t t = mach_absolute_time();
    ((CPUX86State*)cpu_env)->regs[R_EAX] = (int)(t & 0xffffffff);
    ((CPUX86State*)cpu_env)->regs[R_EDX] = (int)((t >> 32) & 0xffffffff);
#else
    qerror("do_nanotime unimplemented");
#endif
}

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

/********************************************************************
 *   do_commpage - called by the main cpu loop
 */
void
do_commpage(void *cpu_env, int num, uint32_t arg1, uint32_t arg2, uint32_t arg3,
                uint32_t arg4, uint32_t arg5, uint32_t arg6, uint32_t arg7,
                uint32_t arg8)
{
    int i, found = 0;

    arg1 = tswap32(arg1);
    arg2 = tswap32(arg2);
    arg3 = tswap32(arg3);
    arg4 = tswap32(arg4);
    arg5 = tswap32(arg5);
    arg6 = tswap32(arg6);
    arg7 = tswap32(arg7);
    arg8 = tswap32(arg8);

    num = num-COMMPAGE_START-2;

    for(i = 0; i < ARRAY_SIZE(commpage_entries); i++) {
        if( num == commpage_code_num(&commpage_entries[i]) )
        {
            DPRINTF("commpage: %s %s\n", commpage_entries[i].name, commpage_is_indirect(&commpage_entries[i]) ? "[indirect]" : "[direct]");
            found = 1;
            if(commpage_is_indirect(&commpage_entries[i]))
            {
                commpage_indirect_function_t function = (commpage_indirect_function_t)commpage_entries[i].function;
                function(cpu_env, num, arg1, arg2, arg3,
                    arg4, arg5, arg6, arg7, arg8);
            }
            else
            {
                commpage_entries[i].function(arg1, arg2, arg3,
                    arg4, arg5, arg6, arg7, arg8);
            }
            break;
        }
    }

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