aboutsummaryrefslogtreecommitdiff
path: root/src/interface/linux/linux_api.c
blob: fa694330eec26682c078a58b0cc0b4116e59b784 (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
 * Copyright (C) 2010 Piotr JaroszyƄski <p.jaroszynski@gmail.com>.
 * Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * 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 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.
 */

#define _GNU_SOURCE
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <fcntl.h>
#include <time.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <ipxe/linux_api.h>
#include <ipxe/slirp.h>

#ifdef HAVE_LIBSLIRP
#include <slirp/libslirp.h>
#endif

#undef static_assert
#define static_assert(x) _Static_assert(x, #x)

/** @file
 *
 * Linux host API
 *
 */

/** Construct prefixed symbol name */
#define _C1( x, y ) x ## y
#define _C2( x, y ) _C1 ( x, y )

/** Construct prefixed symbol name for iPXE symbols */
#define IPXE_SYM( symbol ) _C2 ( SYMBOL_PREFIX, symbol )

/** Provide a prefixed symbol alias visible to iPXE code */
#define PROVIDE_IPXE_SYM( symbol )					\
	extern typeof ( symbol ) IPXE_SYM ( symbol )			\
		__attribute__ (( alias ( #symbol) ))

/** Most recent system call error */
int linux_errno __attribute__ (( nocommon ));

/******************************************************************************
 *
 * Host entry point
 *
 ******************************************************************************
 */

extern int IPXE_SYM ( _linux_start ) ( int argc, char **argv );

/**
 * Main entry point
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Exit status
 */
int main ( int argc, char **argv ) {

	return IPXE_SYM ( _linux_start ) ( argc, argv );
}

/******************************************************************************
 *
 * System call wrappers
 *
 ******************************************************************************
 */

/**
 * Wrap open()
 *
 */
int __asmcall linux_open ( const char *pathname, int flags, ... ) {
	va_list args;
	mode_t mode;
	int ret;

	va_start ( args, flags );
	mode = va_arg ( args, mode_t );
	va_end ( args );
	ret = open ( pathname, flags, mode );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap close()
 *
 */
int __asmcall linux_close ( int fd ) {
	int ret;

	ret = close ( fd );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap lseek()
 *
 */
off_t __asmcall linux_lseek ( int fd, off_t offset, int whence ) {
	off_t ret;

	ret = lseek ( fd, offset, whence );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap read()
 *
 */
ssize_t __asmcall linux_read ( int fd, void *buf, size_t count ) {
	ssize_t ret;

	ret = read ( fd, buf, count );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap write()
 *
 */
ssize_t __asmcall linux_write ( int fd, const void *buf, size_t count ) {
	ssize_t ret;

	ret = write ( fd, buf, count );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap fcntl()
 *
 */
int __asmcall linux_fcntl ( int fd, int cmd, ... ) {
	va_list args;
	long arg;
	int ret;

	va_start ( args, cmd );
	arg = va_arg ( args, long );
	va_end ( args );
	ret = fcntl ( fd, cmd, arg );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap ioctl()
 *
 */
int __asmcall linux_ioctl ( int fd, unsigned long request, ... ) {
	va_list args;
	void *arg;
	int ret;

	va_start ( args, request );
	arg = va_arg ( args, void * );
	va_end ( args );
	ret = ioctl ( fd, request, arg );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap poll()
 *
 */
int __asmcall linux_poll ( struct pollfd *fds, unsigned int nfds,
			   int timeout ) {
	int ret;

	ret = poll ( fds, nfds, timeout );
	if ( ret == -1 )
		linux_errno = errno;
}

/**
 * Wrap nanosleep()
 *
 */
int __asmcall linux_nanosleep ( const struct timespec *req,
				struct timespec *rem ) {
	int ret;

	ret = nanosleep ( req, rem );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap usleep()
 *
 */
int __asmcall linux_usleep ( unsigned int usec ) {
	int ret;

	ret = usleep ( usec );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap gettimeofday()
 *
 */
int __asmcall linux_gettimeofday ( struct timeval *tv, struct timezone *tz ) {
	int ret;

	ret = gettimeofday ( tv, tz );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap mmap()
 *
*/
void * __asmcall linux_mmap ( void *addr, size_t length, int prot, int flags,
			      int fd, off_t offset ) {
	void *ret;

	ret = mmap ( addr, length, prot, flags, fd, offset );
	if ( ret == MAP_FAILED )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap mremap()
 *
 */
void * __asmcall linux_mremap ( void *old_address, size_t old_size,
				size_t new_size, int flags, ... ) {
	va_list args;
	void *new_address;
	void *ret;

	va_start ( args, flags );
	new_address = va_arg ( args, void * );
	va_end ( args );
	ret = mremap ( old_address, old_size, new_size, flags, new_address );
	if ( ret == MAP_FAILED )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap munmap()
 *
 */
int __asmcall linux_munmap ( void *addr, size_t length ) {
	int ret;

	ret = munmap ( addr, length );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap socket()
 *
 */
int __asmcall linux_socket ( int domain, int type, int protocol ) {
	int ret;

	ret = socket ( domain, type, protocol );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap bind()
 *
 */
int __asmcall linux_bind ( int sockfd, const struct sockaddr *addr,
			   size_t addrlen ) {
	int ret;

	ret = bind ( sockfd, addr, addrlen );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/**
 * Wrap sendto()
 *
 */
ssize_t __asmcall linux_sendto ( int sockfd, const void *buf, size_t len,
				 int flags, const struct sockaddr *dest_addr,
				 size_t addrlen ) {
	ssize_t ret;

	ret = sendto ( sockfd, buf, len, flags, dest_addr, addrlen );
	if ( ret == -1 )
		linux_errno = errno;
	return ret;
}

/******************************************************************************
 *
 * C library wrappers
 *
 ******************************************************************************
 */

/**
 * Wrap strerror()
 *
 */
const char * __asmcall linux_strerror ( int linux_errno ) {

	return strerror ( linux_errno );
}

/******************************************************************************
 *
 * libslirp wrappers
 *
 ******************************************************************************
 */

#ifdef HAVE_LIBSLIRP

/**
 * Wrap slirp_new()
 *
 */
struct Slirp * __asmcall
linux_slirp_new ( const struct slirp_config *config,
		  const struct slirp_callbacks *callbacks, void *opaque ) {
	const union {
		struct slirp_callbacks callbacks;
		SlirpCb cb;
	} *u = ( ( typeof ( u ) ) callbacks );
	SlirpConfig cfg;
	Slirp *slirp;

	/* Translate configuration */
	memset ( &cfg, 0, sizeof ( cfg ) );
	cfg.version = config->version;
	cfg.restricted = config->restricted;
	cfg.in_enabled = config->in_enabled;
	cfg.vnetwork = config->vnetwork;
	cfg.vnetmask = config->vnetmask;
	cfg.vhost = config->vhost;
	cfg.in6_enabled = config->in6_enabled;
	memcpy ( &cfg.vprefix_addr6, &config->vprefix_addr6,
		 sizeof ( cfg.vprefix_addr6 ) );
	cfg.vprefix_len = config->vprefix_len;
	memcpy ( &cfg.vhost6, &config->vhost6, sizeof ( cfg.vhost6 ) );
	cfg.vhostname = config->vhostname;
	cfg.tftp_server_name = config->tftp_server_name;
	cfg.tftp_path = config->tftp_path;
	cfg.bootfile = config->bootfile;
	cfg.vdhcp_start = config->vdhcp_start;
	cfg.vnameserver = config->vnameserver;
	memcpy ( &cfg.vnameserver6, &config->vnameserver6,
		 sizeof ( cfg.vnameserver6 ) );
	cfg.vdnssearch = config->vdnssearch;
	cfg.vdomainname = config->vdomainname;
	cfg.if_mtu = config->if_mtu;
	cfg.if_mru = config->if_mru;
	cfg.disable_host_loopback = config->disable_host_loopback;
	cfg.enable_emu = config->enable_emu;

	/* Validate callback structure */
	static_assert ( &u->cb.send_packet == &u->callbacks.send_packet );
	static_assert ( &u->cb.guest_error == &u->callbacks.guest_error );
	static_assert ( &u->cb.clock_get_ns == &u->callbacks.clock_get_ns );
	static_assert ( &u->cb.timer_new == &u->callbacks.timer_new );
	static_assert ( &u->cb.timer_free == &u->callbacks.timer_free );
	static_assert ( &u->cb.timer_mod == &u->callbacks.timer_mod );
	static_assert ( &u->cb.register_poll_fd ==
			&u->callbacks.register_poll_fd );
	static_assert ( &u->cb.unregister_poll_fd ==
			&u->callbacks.unregister_poll_fd );
	static_assert ( &u->cb.notify == &u->callbacks.notify );

	/* Create device */
	slirp = slirp_new ( &cfg, &u->cb, opaque );

	return slirp;
}

/**
 * Wrap slirp_cleanup()
 *
 */
void __asmcall linux_slirp_cleanup ( struct Slirp *slirp ) {

	slirp_cleanup ( slirp );
}

/**
 * Wrap slirp_input()
 *
 */
void __asmcall linux_slirp_input ( struct Slirp *slirp, const uint8_t *pkt,
				   int pkt_len ) {

	slirp_input ( slirp, pkt, pkt_len );
}

/**
 * Wrap slirp_pollfds_fill()
 *
 */
void __asmcall
linux_slirp_pollfds_fill ( struct Slirp *slirp, uint32_t *timeout,
			   int ( __asmcall * add_poll ) ( int fd, int events,
							  void *opaque ),
			   void *opaque ) {

	slirp_pollfds_fill ( slirp, timeout, add_poll, opaque );
}

/**
 * Wrap slirp_pollfds_poll()
 *
 */
void __asmcall
linux_slirp_pollfds_poll ( struct Slirp *slirp, int select_error,
			   int ( __asmcall * get_revents ) ( int idx,
							     void *opaque ),
			   void *opaque ) {

	slirp_pollfds_poll ( slirp, select_error, get_revents, opaque );
}

#else /* HAVE_LIBSLIRP */

struct Slirp * __asmcall
linux_slirp_new ( const struct slirp_config *config,
		  const struct slirp_callbacks *callbacks, void *opaque ) {
	return NULL;
}

void __asmcall linux_slirp_cleanup ( struct Slirp *slirp ) {
}

void __asmcall linux_slirp_input ( struct Slirp *slirp, const uint8_t *pkt,
				   int pkt_len ) {
}

void __asmcall
linux_slirp_pollfds_fill ( struct Slirp *slirp, uint32_t *timeout,
			   int ( __asmcall * add_poll ) ( int fd, int events,
							  void *opaque ),
			   void *opaque ) {
}

void __asmcall
linux_slirp_pollfds_poll ( struct Slirp *slirp, int select_error,
			   int ( __asmcall * get_revents ) ( int idx,
							     void *opaque ),
			   void *opaque ) {
}

#endif /* HAVE_LIBSLIRP */

/******************************************************************************
 *
 * Symbol aliases
 *
 ******************************************************************************
 */

PROVIDE_IPXE_SYM ( linux_errno );
PROVIDE_IPXE_SYM ( linux_open );
PROVIDE_IPXE_SYM ( linux_close );
PROVIDE_IPXE_SYM ( linux_lseek );
PROVIDE_IPXE_SYM ( linux_read );
PROVIDE_IPXE_SYM ( linux_write );
PROVIDE_IPXE_SYM ( linux_fcntl );
PROVIDE_IPXE_SYM ( linux_ioctl );
PROVIDE_IPXE_SYM ( linux_poll );
PROVIDE_IPXE_SYM ( linux_nanosleep );
PROVIDE_IPXE_SYM ( linux_usleep );
PROVIDE_IPXE_SYM ( linux_gettimeofday );
PROVIDE_IPXE_SYM ( linux_mmap );
PROVIDE_IPXE_SYM ( linux_mremap );
PROVIDE_IPXE_SYM ( linux_munmap );
PROVIDE_IPXE_SYM ( linux_socket );
PROVIDE_IPXE_SYM ( linux_bind );
PROVIDE_IPXE_SYM ( linux_sendto );
PROVIDE_IPXE_SYM ( linux_strerror );
PROVIDE_IPXE_SYM ( linux_slirp_new );
PROVIDE_IPXE_SYM ( linux_slirp_cleanup );
PROVIDE_IPXE_SYM ( linux_slirp_input );
PROVIDE_IPXE_SYM ( linux_slirp_pollfds_fill );
PROVIDE_IPXE_SYM ( linux_slirp_pollfds_poll );