18
18
*/
19
19
#include "qemu/osdep.h"
20
20
21
+ #include <sys/syslimits.h>
22
+ #include <fcntl.h>
23
+
21
24
#include "qemu.h"
22
25
#include "qemu-common.h"
23
26
#include "translate-all.h"
@@ -367,11 +370,12 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
367
370
int flags , int fd , abi_ulong offset )
368
371
{
369
372
abi_ulong ret , end , real_start , real_end , retaddr , host_offset , host_len ;
373
+ char filePath [PATH_MAX ];
370
374
371
375
mmap_lock ();
372
376
#ifdef DEBUG_MMAP
373
377
{
374
- printf ("mmap : start=0x" TARGET_ABI_FMT_lx
378
+ printf ("\nmmap : start=0x" TARGET_ABI_FMT_lx
375
379
" len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=" ,
376
380
start , len ,
377
381
prot & PROT_READ ? 'r' : '-' ,
@@ -393,9 +397,29 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
393
397
break ;
394
398
}
395
399
printf ("fd=%d offset=" TARGET_ABI_FMT_lx "\n" , fd , offset );
400
+ if (fcntl (fd , F_GETPATH , filePath ) != -1 ) {
401
+ printf ("fd name=%s\n" , filePath );
402
+ }
396
403
}
397
404
#endif
398
405
406
+ /* macOS cannot mmap /dev/zero. It needs `MAP_ANON` with an fd of -1
407
+ * Catch this scenario and fix
408
+ * https://stackoverflow.com/questions/28679577/mmapping-dev-zero-on-mac-osx-gives-invalid-argument
409
+ * https://stackoverflow.com/questions/1188757/retrieve-filename-from-file-descriptor-in-c
410
+ */
411
+ if (fd != -1
412
+ && !(flags & MAP_ANON )
413
+ && fcntl (fd , F_GETPATH , filePath ) != -1
414
+ && strcmp (filePath , "/dev/zero" ) == 0
415
+ ) {
416
+ fd = -1 ;
417
+ flags |= MAP_ANON ;
418
+ #ifdef DEBUG_MMAP
419
+ printf ("Changed fd and flags to support mmap /dev/zero\n" );
420
+ #endif
421
+ }
422
+
399
423
if (offset & ~TARGET_PAGE_MASK ) {
400
424
errno = EINVAL ;
401
425
goto fail ;
0 commit comments