ffi_arm.d 17.3 KB
Newer Older
1
2
/* -*- mode: c -*- */
/*
3
    ffi_arm.c -- Nonportable component of the FFI
4
5
6
*/
/*
    Copyright (c) 2005, Juan Jose Garcia Ripoll.
7
    Copyright (c) 2020, Jean-Claude Beaudoin.
8
9
10
11
12
13
14
15
16
17
18
19
20
21

    MKCL is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.

    See file '../../../Copyright' for full details.
*/

#include <mkcl/mkcl.h>
#include <mkcl/internal.h>
#include <string.h>
#include <sys/mman.h>

22
23
24
25
26
27
28
29
30
31
32

#define MAX_CORE_REGISTERS 4
#define MAX_VFP_REGISTERS 16

struct mkcl_fficall_reg {
  long r[MAX_CORE_REGISTERS];
  int core_register_count;
  union {
    float s[MAX_VFP_REGISTERS];
    double d[MAX_VFP_REGISTERS/2];
  } vfp;
33
34
  bool vfp_register_allocated[MAX_VFP_REGISTERS];
  int vfp_register_least_unallocated;
35
36
37
38
39
40
41
42
43
};

struct mkcl_fficall_reg *
mkcl_fficall_prepare_extra(MKCL, struct mkcl_fficall_reg *registers)
{
  if (registers == 0) {
    registers = mkcl_alloc(env, sizeof(*registers));
  }
  registers->core_register_count = 0;
44
45
46
47
48
  registers->vfp_register_least_unallocated = 0;
  {
    int i = 0;
    for (; i < MAX_VFP_REGISTERS; i++) registers->vfp_register_allocated[i] = FALSE;
  }
49
50
51
  return registers;
}

52
53
54
void
mkcl_fficall_push_arg(MKCL, union mkcl_ffi_values *data, enum mkcl_ffi_tag type)
{
55
56
57
58
  long word;
  struct mkcl_fficall *fficall = env->fficall;
  struct mkcl_fficall_reg *registers = fficall->registers;

59
  switch (type) {
60
61
62
  case MKCL_FFI_POINTER_VOID: word = (intptr_t) data->pv; goto WORD_WIDE;
  case MKCL_FFI_CSTRING: word = (intptr_t) data->pc; goto WORD_WIDE;
  case MKCL_FFI_OBJECT: word = (intptr_t) data->o; goto WORD_WIDE;
63
64
65
66
67
68
  case MKCL_FFI_CHAR: word = data->c;	goto WORD_WIDE;
  case MKCL_FFI_UNSIGNED_CHAR: word = data->uc; goto WORD_WIDE;
  case MKCL_FFI_BYTE: word = data->b; goto WORD_WIDE;
  case MKCL_FFI_UNSIGNED_BYTE: word = data->ub; goto WORD_WIDE;
  case MKCL_FFI_SHORT: word = data->s; goto WORD_WIDE;
  case MKCL_FFI_UNSIGNED_SHORT: word = data->us; goto WORD_WIDE;
69
70
71
72
  case MKCL_FFI_INT:
  case MKCL_FFI_LONG:
  case MKCL_FFI_UNSIGNED_INT:
  case MKCL_FFI_UNSIGNED_LONG:
73
74
75
76
77
78
79
80
    word = data->l;
  WORD_WIDE:
    if (registers->core_register_count < MAX_CORE_REGISTERS) {
      registers->r[registers->core_register_count++] = word;
    } else {
      mkcl_fficall_align4(env);
      mkcl_fficall_push_bytes(env, &word, sizeof(long));
    }
81
    break;
82
  case MKCL_FFI_LONG_DOUBLE:
83
  case MKCL_FFI_DOUBLE:
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
    if (registers->vfp_register_least_unallocated < (MAX_VFP_REGISTERS - 1)) {
      int least_unallocated_pair = registers->vfp_register_least_unallocated;
      if (least_unallocated_pair & 1) least_unallocated_pair++; /* round up to next pair */
      for (; (least_unallocated_pair < MAX_VFP_REGISTERS)
	     && (registers->vfp_register_allocated[least_unallocated_pair]
		 || registers->vfp_register_allocated[least_unallocated_pair + 1])
	     ; least_unallocated_pair += 2);
      if (least_unallocated_pair < MAX_VFP_REGISTERS) {
	registers->vfp.d[least_unallocated_pair / 2] = data->d;
	registers->vfp_register_allocated[least_unallocated_pair++] = TRUE;
	registers->vfp_register_allocated[least_unallocated_pair++] = TRUE;
	{
	  int i = least_unallocated_pair;

	  for (i++; (i < MAX_VFP_REGISTERS) && registers->vfp_register_allocated[i]; i++);
	  registers->vfp_register_least_unallocated = i;
	}
      } else {
	registers->vfp_register_least_unallocated = MAX_VFP_REGISTERS; /* this ends back-filling */
	mkcl_fficall_align8(env);
	mkcl_fficall_push_bytes(env, &data->d, sizeof(double));
      }
    } else {
      registers->vfp_register_least_unallocated = MAX_VFP_REGISTERS; /* this ends back-filling */
      mkcl_fficall_align8(env);
      mkcl_fficall_push_bytes(env, &data->d, sizeof(double));
    }
111
112
    break;
  case MKCL_FFI_FLOAT:
113
114
115
116
117
118
119
120
121
122
    if (registers->vfp_register_least_unallocated < MAX_VFP_REGISTERS) {
      int i = registers->vfp_register_least_unallocated;
      registers->vfp.s[i] = data->f;
      registers->vfp_register_allocated[i] = TRUE;
      for (i++; (i < MAX_VFP_REGISTERS) && registers->vfp_register_allocated[i]; i++);
      registers->vfp_register_least_unallocated = i;
    } else {
      mkcl_fficall_align4(env);
      mkcl_fficall_push_bytes(env, &data->f, sizeof(float));
    }
123
124
125
    break;
  case MKCL_FFI_UNSIGNED_LONG_LONG:
  case MKCL_FFI_LONG_LONG:
126
127
128
129
130
131
132
    if (registers->core_register_count < (MAX_CORE_REGISTERS - 1)) {
      registers->r[registers->core_register_count++] = data->l2[0];
      registers->r[registers->core_register_count++] = data->l2[1];
    } else {
      mkcl_fficall_align8(env);
      mkcl_fficall_push_bytes(env, &data->ull, sizeof(unsigned long long));
    }
133
134
135
136
137
138
139
140
141
142
143
    break;
  case MKCL_FFI_VOID:
    mkcl_FEerror(env, "VOID is not a valid argument type for a C function", 0);
  default:
    mkcl_FEerror(env, "Unknown argument type for a C function", 0);
  }
}

void
mkcl_fficall_execute(MKCL, void *f_ptr, struct mkcl_fficall *fficall, enum mkcl_ffi_tag return_type)
{
144
  mkcl_word * const args_buffer = (mkcl_word *) fficall->buffer;
145
146
  char* stack_p;
  size_t bufsize;
147
148
  mkcl_index btop;
  struct mkcl_fficall_reg * regs = fficall->registers;
149
150
151

  mkcl_fficall_align16(env); /* Size of a cache line. */
  bufsize = fficall->buffer_sp - fficall->buffer;
152
  btop = bufsize / sizeof(mkcl_word);
153

154
155
156
  typedef long (*word_integral_ffunc)(long a0, long a1, long a2, long a3,
				      double d0, double d1, double d2, double d3,
				      double d4, double d5, double d6, double d7);
157

158
159
160
  typedef long long (*long_long_ffunc)(long a0, long a1, long a2, long a3,
				       double d0, double d1, double d2, double d3,
				       double d4, double d5, double d6, double d7);
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
  typedef void * (*ptr_ffunc)(long a0, long a1, long a2, long a3,
			      double d0, double d1, double d2, double d3,
			      double d4, double d5, double d6, double d7);

  typedef float (*float_ffunc)(long a0, long a1, long a2, long a3,
			       double d0, double d1, double d2, double d3,
			       double d4, double d5, double d6, double d7);

  typedef double (*double_ffunc)(long a0, long a1, long a2, long a3,
				 double d0, double d1, double d2, double d3,
				 double d4, double d5, double d6, double d7);

  typedef long double (*long_double_ffunc)(long a0, long a1, long a2, long a3,
					   double d0, double d1, double d2, double d3,
					   double d4, double d5, double d6, double d7);

  typedef void (*void_ffunc)(long a0, long a1, long a2, long a3,
			     double d0, double d1, double d2, double d3,
			     double d4, double d5, double d6, double d7);

  /* Save current stack pointer and then push stack based arguments. */
  {
    mkcl_word args_on_stack[btop]; /* VLA */

    for (; btop; btop--)
      args_on_stack[btop] = args_buffer[btop]; /* copy overflow args to the tip of the stack. */

    if (return_type <= MKCL_FFI_UNSIGNED_LONG) {
      fficall->output.l
191
192
193
	= ((word_integral_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
					 regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
					 regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
194
195
    } else if ((return_type == MKCL_FFI_POINTER_VOID)
	       || (return_type == MKCL_FFI_CSTRING)
196
	       ||  (return_type == MKCL_FFI_OBJECT)) {
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
      fficall->output.pv
	= ((ptr_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
			       regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
			       regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    } else if (return_type == MKCL_FFI_FLOAT) {
      fficall->output.f
	= ((float_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
				 regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
				 regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    } else if (return_type == MKCL_FFI_DOUBLE) {
      fficall->output.d
	= ((double_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
				  regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
				  regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    } else if (return_type == MKCL_FFI_LONG_DOUBLE) {
      fficall->output.ld
	= ((long_double_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
				       regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
				       regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    }
    else if (return_type == MKCL_FFI_VOID) {
      ((void_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
			    regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
			    regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    }
    else if ((return_type == MKCL_FFI_LONG_LONG)
	     || (return_type == MKCL_FFI_UNSIGNED_LONG_LONG)) {
      fficall->output.ll
	= ((long_long_ffunc) f_ptr) (regs->r[0], regs->r[1], regs->r[2], regs->r[3],
				     regs->vfp.d[0], regs->vfp.d[1], regs->vfp.d[2], regs->vfp.d[3],
				     regs->vfp.d[4], regs->vfp.d[5], regs->vfp.d[6], regs->vfp.d[7]);
    }
    else {
      mkcl_FEerror(env, "Unknown C function return type", 0);
    }
  }
233
234
235
236
237
238
}


static const mkcl_base_string_object(mkcl_dynamic_callback_import_thread_name__obj_, "mkcl_dynamic_callback_execute");
static const mkcl_object mkcl_dynamic_callback_import_thread_name = (mkcl_object) &mkcl_dynamic_callback_import_thread_name__obj_;

239
static union mkcl_ffi_values
240
mkcl_dynamic_callback_execute(long a1, long a2, long a3, long a4,
241
242
243
			      double d0, double d1, double d2, double d3,
			      double d4, double d5, double d6, double d7,
			      mkcl_object cbk_info, char *arg_buffer)
244
245
246
247
248
249
{
  char stack_mark = 0;
  mkcl_object result;
  mkcl_env env = MKCL_ENV();
  mkcl_env imported_env = NULL;

250
251
252
253
  mkcl_object fun = MKCL_CAR(cbk_info);
  mkcl_object rtype = MKCL_CADR(cbk_info);
  mkcl_object argtypes = MKCL_CADDR(cbk_info);

254
255
  arg_buffer += sizeof(long); /* Skip saved LR (a.k.a.: return address) */
  arg_buffer += sizeof(long); /* Skip saved r4 */
256

257
258
259
260
261
  if (env == NULL)
    {
      env = imported_env = mkcl_import_current_thread(mkcl_dynamic_callback_import_thread_name, mk_cl_Cnil, NULL, NULL);
      if (imported_env == NULL)
        /* In the normal case we always set errno = 0, so this is clearly an error situation. */
262
	{ union mkcl_ffi_values val; val.ll = 0; errno = ENOMEM; return val; }
263
264
265
266
    }

  MKCL_BUILD_TEMP_STACK_FRAME(env, frame, aux);

267
268
269
  for (; !mkcl_endp(env, argtypes); argtypes = MKCL_CDR(argtypes)) {
    enum mkcl_ffi_tag tag = mkcl_foreign_type_code(env, MKCL_CAR(argtypes));
    mkcl_index size = mkcl_fixnum_to_word(mk_si_size_of_foreign_elt_type(env, MKCL_CAR(argtypes)));
270
271
272
273
274
    result = mkcl_foreign_ref_elt(env, arg_buffer, tag);
    {
      mkcl_index sp = (size + 0x03) & ~((mkcl_index) 0x03);
      arg_buffer += (sp);
    }
275
    mkcl_temp_stack_frame_push(env, frame, result);
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  }

  if (imported_env)
    {
      mkcl_object thread = env->own_thread;

      MKCL_CATCH_ALL_BEGIN(env) {
	MKCL_SETUP_CALL_STACK_ROOT_GUARD(env);
	mkcl_setup_thread_lisp_context(env, &stack_mark);
	mkcl_register_thread_as_active(env, thread);
	mkcl_enable_interrupts(env);

	result = mkcl_apply_from_temp_stack_frame(env, frame, fun);

	mkcl_disable_interrupts(env);
	mkcl_cleanup_thread_lisp_context(env);
      } MKCL_CATCH_ALL_END;
      thread->thread.status = mkcl_thread_done;
    }
  else
    result = mkcl_apply_from_temp_stack_frame(env, frame, fun);
  mkcl_temp_stack_frame_close(env, frame);

299
300
301
  {
    union mkcl_ffi_values output;
    enum mkcl_ffi_tag tag = mkcl_foreign_type_code(env, rtype);
302

303
304
    memset(&output, 0, sizeof(output));
    mkcl_foreign_set_elt(env, &output, tag, result);
305

306
307
    if (imported_env)
      mkcl_release_current_thread(imported_env);
308

309
310
    errno = 0; /* See above for the necessity of this. */
    return output;
311
312
313
  }
}

314
static void
315
mkcl_void_dynamic_callback_execute(long a1, long a2, long a3, long a4,
316
317
318
319
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
320
  (void) mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
321
322
323
}

static mkcl_int8_t
324
mkcl_byte_dynamic_callback_execute(long a1, long a2, long a3, long a4,
325
326
327
328
329
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
330
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
331
332
333
334
  return val.b;
}

static short
335
mkcl_short_dynamic_callback_execute(long a1, long a2, long a3, long a4,
336
337
338
339
340
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
341
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
342
343
344
345
  return val.s;
}

static long
346
mkcl_long_dynamic_callback_execute(long a1, long a2, long a3, long a4,
347
348
349
350
351
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
352
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
353
354
355
356
  return val.l;
}

static long long
357
mkcl_long_long_dynamic_callback_execute(long a1, long a2, long a3, long a4,
358
359
360
361
362
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
363
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
364
365
366
367
  return val.ll;
}

static float
368
mkcl_float_dynamic_callback_execute(long a1, long a2, long a3, long a4,
369
370
371
372
373
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
374
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
375
376
377
378
  return val.f;
}

static double
379
mkcl_double_dynamic_callback_execute(long a1, long a2, long a3, long a4,
380
381
382
383
384
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
385
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
386
387
388
389
  return val.d;
}

static long double
390
mkcl_long_double_dynamic_callback_execute(long a1, long a2, long a3, long a4,
391
392
393
394
395
				   double d0, double d1, double d2, double d3,
				   double d4, double d5, double d6, double d7,
				   mkcl_object cbk_info, char *arg_buffer)
{
  union mkcl_ffi_values val
396
    = mkcl_dynamic_callback_execute(a1, a2, a3, a4, d0, d1, d2, d3, d4, d5, d6, d7, cbk_info, arg_buffer);
397
398
399
400
401
  return val.ld;
}



402
403
404
405
406
407
408
409
410
411
412
413
414
415

void *
mkcl_dynamic_callback_make(MKCL, mkcl_object data, enum mkcl_ffi_calling_convention cc_type)
{
  unsigned char * buf = mkcl_alloc_callback_block(env);
  unsigned char * ip = buf; /* the instruction pointer (ip) */
  union { unsigned char b[4]; void * p; unsigned long l; unsigned short s; } imm; /* a staging buffer for immediate data */

#define i(byte) *(ip++) = (byte)
#define immed_ptr(val_ptr) imm.p = (val_ptr); i(imm.b[0]); i(imm.b[1]); i(imm.b[2]); i(imm.b[3]);
#define immed16(val_short) imm.s = (val_short);	i(imm.b[0]); i(imm.b[1]);
#define immed32(val_long) imm.l = (val_long); i(imm.b[0]); i(imm.b[1]); i(imm.b[2]); i(imm.b[3]);
    

416
417
  enum mkcl_ffi_tag tag = mkcl_foreign_type_code(env, MKCL_CADR(data));
  void * fptr = 0;
418

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  switch (tag) {
  case MKCL_FFI_CHAR:
  case MKCL_FFI_UNSIGNED_CHAR:
  case MKCL_FFI_BYTE:
  case MKCL_FFI_UNSIGNED_BYTE:
    fptr = mkcl_byte_dynamic_callback_execute;
    break;
  case MKCL_FFI_SHORT:
  case MKCL_FFI_UNSIGNED_SHORT:
    fptr = mkcl_short_dynamic_callback_execute;
    break;
  case MKCL_FFI_POINTER_VOID:
  case MKCL_FFI_OBJECT:
  case MKCL_FFI_CSTRING:
  case MKCL_FFI_INT:
  case MKCL_FFI_UNSIGNED_INT:
  case MKCL_FFI_LONG:
  case MKCL_FFI_UNSIGNED_LONG:
    fptr = mkcl_long_dynamic_callback_execute;
    break;
  case MKCL_FFI_LONG_LONG:
  case MKCL_FFI_UNSIGNED_LONG_LONG:
    fptr = mkcl_long_long_dynamic_callback_execute;
    break;
  case MKCL_FFI_DOUBLE:
    fptr = mkcl_double_dynamic_callback_execute;
    break;
  case MKCL_FFI_LONG_DOUBLE:
    fptr = mkcl_long_double_dynamic_callback_execute;
    break;
  case MKCL_FFI_FLOAT:
    fptr = mkcl_float_dynamic_callback_execute;
    break;
  case MKCL_FFI_VOID:
    fptr = mkcl_void_dynamic_callback_execute;
    break;
  default:
    mkcl_FEerror(env, "Invalid C function callback return type", 0);
457
  }
458

459
460
461
462
463
464
465
466
467
468
469
470
471
  i(0x10); i(0xB5);	/* push	{r4, lr}                               */
  i(0x6C); i(0x46);	/* mov	r4, sp                                 */
  i(0x10); i(0xB4);	/* push    {r4}           @ push stack_marker  */
  i(0x03); i(0x4C);	/* ldr	r4, =0xdeadbeef   @ get cbk_info       */
  i(0x10); i(0xB4);	/* push    {r4}           @ push cbk_info      */
  i(0x03); i(0x4C);	/* ldr	r4, =0xbeefdead   @ get pointer to callback frame provider */
  i(0xA0); i(0x47);	/* blx	r4                @ call through r4    */
  i(0x02); i(0xB0);	/* add	sp, sp, #8        @ remove pushed args */
  i(0x10); i(0xBD);	/* pop	{r4, pc}                               */
  i(0x00); i(0xBF);	/* .align 2                                    */
  immed_ptr(data);
  immed_ptr(fptr);

472
473
474
475
476

  {
    int rc = mprotect(buf, mkcl_core.pagesize, PROT_READ | /* PROT_WRITE | */ PROT_EXEC);
    if (rc)
      mkcl_FElibc_error(env, "mkcl_dynamic_callback_make() failed on mprotect()", 0);
477
478
479
480
  }

  return buf;
}