Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions block/file-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,7 @@ static int handle_aiocb_copy_range(void *opaque)
off_t out_off = aiocb->copy_range.aio_offset2;

while (bytes) {
#ifndef __ANDROID__
ssize_t ret = copy_file_range(aiocb->aio_fildes, &in_off,
aiocb->copy_range.aio_fd2, &out_off,
bytes, 0);
Expand All @@ -2195,6 +2196,7 @@ static int handle_aiocb_copy_range(void *opaque)
}
}
bytes -= ret;
#endif
}
return 0;
}
Expand Down
7 changes: 6 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ int main(void) { return 0; }
EOF
}

if check_define __linux__ ; then
if check_define __linux__ || check_define __ANDROID__ ; then
host_os=linux
elif check_define _WIN32 ; then
host_os=windows
Expand Down Expand Up @@ -427,6 +427,11 @@ else
fi
fi

if check_define __ANDROID__ ; then
rm /data/data/com.termux/files/usr/include/linux/virtio_ring.h
cp /data/data/com.termux/files/home/QEMUAppleSilicon/include/standard-headers/linux/virtio_ring.h /data/data/com.termux/files/usr/include/linux/
fi

# Normalise host CPU name to the values used by Meson cross files and in source
# directories, and set multilib cflags. The canonicalization isn't really
# necessary, because the architectures that we check for should not hit the
Expand Down
44 changes: 42 additions & 2 deletions contrib/ivshmem-server/ivshmem-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
* (at your option) any later version. See the COPYING file in the
* top-level directory.
*/
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#ifdef __ANDROID__
#define SHM_SIZE 4096
#endif

#include "qemu/osdep.h"
#include "qemu/host-utils.h"
#include "qemu/sockets.h"
Expand Down Expand Up @@ -275,7 +282,11 @@ ivshmem_server_init(IvshmemServer *server, const char *unix_sock_path,
return -1;
}

#ifndef __ANDROID__
server->use_shm_open = use_shm_open;
#else
server->use_shm_open = false;
#endif
server->shm_size = shm_size;
server->n_vectors = n_vectors;

Expand All @@ -291,19 +302,44 @@ ivshmem_server_start(IvshmemServer *server)
struct sockaddr_un s_un;
int shm_fd, sock_fd, ret;

#ifdef __ANDROID__
gchar *filename = g_strdup_printf("%s/ivshmem.XXXXXX", server->shm_path);
IVSHMEM_SERVER_DEBUG(server, "Using file-backed shared memory: %s\n", server->shm_path);
shm_fd = mkstemp(filename);
if (shm_fd == -1) {
perror("Failed to create temporary shm file");
g_free(filename);
return -1;
}
unlink(filename);
g_free(filename);

if (ftruncate(shm_fd, SHM_SIZE) == -1) {
perror("Failed to set size of shared memory");
close(shm_fd);
return -1;
}

void *shm_ptr = mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (shm_ptr == MAP_FAILED) {
perror("Failed to map shared memory");
close(shm_fd);
return -1;
}
#else
/* open shm file */
if (server->use_shm_open) {
IVSHMEM_SERVER_DEBUG(server, "Using POSIX shared memory: %s\n",
server->shm_path);
shm_fd = shm_open(server->shm_path, O_CREAT | O_RDWR, S_IRWXU);
} else {
gchar *filename = g_strdup_printf("%s/ivshmem.XXXXXX", server->shm_path);
IVSHMEM_SERVER_DEBUG(server, "Using file-backed shared memory: %s\n",
server->shm_path);
IVSHMEM_SERVER_DEBUG(server, "Using file-backed shared memory: %s\n", server->shm_path);
shm_fd = mkstemp(filename);
unlink(filename);
g_free(filename);
}
#endif

if (shm_fd < 0) {
fprintf(stderr, "cannot open shm file %s: %s\n", server->shm_path,
Expand Down Expand Up @@ -354,7 +390,9 @@ ivshmem_server_start(IvshmemServer *server)
close(sock_fd);
err_close_shm:
if (server->use_shm_open) {
#ifndef __ANDROID__
shm_unlink(server->shm_path);
#endif
}
close(shm_fd);
return -1;
Expand All @@ -374,7 +412,9 @@ ivshmem_server_close(IvshmemServer *server)

unlink(server->unix_sock_path);
if (server->use_shm_open) {
#ifndef __ANDROID__
shm_unlink(server->shm_path);
#endif
}
close(server->sock_fd);
close(server->shm_fd);
Expand Down
12 changes: 12 additions & 0 deletions fsdev/9p-marshal.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
#ifdef __ANDROID__
#ifdef st_atime_nsec
#undef st_atime_nsec
#endif
#ifdef st_mtime_nsec
#undef st_mtime_nsec
#endif
#ifdef st_ctime_nsec
#undef st_ctime_nsec
#endif
#endif

#ifndef QEMU_9P_MARSHAL_H
#define QEMU_9P_MARSHAL_H

Expand Down
8 changes: 7 additions & 1 deletion hw/9pfs/9p-local.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Not so fast! You might want to read the 9p developer docs first:
* https://wiki.qemu.org/Documentation/9p
*/

#include <sys/mman.h>
#include "qemu/osdep.h"
#include "9p.h"
#include "9p-local.h"
Expand Down Expand Up @@ -626,8 +626,14 @@ static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
* We want to ensure that we don't leave dirty pages in the cache
* after write when writeout=immediate is specified.
*/
#ifndef __ANDROID__
sync_file_range(fs->fd, offset, ret,
SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
#else
(void)fs;
(void)offset;
(void)ret;
#endif
}
#endif
return ret;
Expand Down
29 changes: 29 additions & 0 deletions subprojects/libvhost-user/libvhost-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#define _GNU_SOURCE
#endif

#include <sys/stat.h>

/* this code avoids GLib dependency */
#include <stdlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -1876,15 +1878,42 @@ vu_inflight_queue_size(uint16_t queue_size)
}

#ifdef MFD_ALLOW_SEALING

#ifdef __ANDROID__
#ifndef __NR_memfd_create
#define __NR_memfd_create 319
#endif

static inline int safe_memfd_create(const char *name, unsigned int flags) {
#ifdef __ANDROID__
return syscall(__NR_memfd_create, name, flags);
#else
return memfd_create(name, flags);
#endif
}
#endif

static void *
memfd_alloc(const char *name, size_t size, unsigned int flags, int *fd)
{
void *ptr;
int ret;

#ifdef __ANDROID__
*fd = safe_memfd_create(name, MFD_ALLOW_SEALING);
#else
*fd = memfd_create(name, MFD_ALLOW_SEALING);
#endif
if (*fd < 0) {
#ifdef __ANDROID__
char path[] = "/data/data/com.termux/files/usr/tmp/memfd-XXXXXX";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break Android compilation outside of Termux (e.g. with the android NDK)?

*fd = mkstemp(path);
if (*fd < 0)
return NULL;
unlink(path);
#else
return NULL;
#endif
}

ret = ftruncate(*fd, size);
Expand Down
8 changes: 8 additions & 0 deletions tests/qtest/ivshmem-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ static void cleanup(void)
}

if (tmpshm) {
#ifndef __ANDROID__
shm_unlink(tmpshm);
#endif
g_free(tmpshm);
tmpshm = NULL;
}
Expand Down Expand Up @@ -463,8 +465,14 @@ static gchar *mktempshm(int size, int *fd)
gchar *name;

name = g_strdup_printf("/qtest-%u-%u", getpid(), g_test_rand_int());
#ifndef __ANDROID__
*fd = shm_open(name, O_CREAT|O_RDWR|O_EXCL,
S_IRWXU|S_IRWXG|S_IRWXO);
#else
gchar *filename = g_strdup_printf("/data/data/com.termux/files/usr/tmp/%s", name);
*fd = open(filename, O_CREAT | O_RDWR | O_EXCL, 0600);
g_free(filename);
#endif
if (*fd > 0) {
g_assert(ftruncate(*fd, size) == 0);
return name;
Expand Down
21 changes: 21 additions & 0 deletions util/oslib-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@

#include "qemu/mmap-alloc.h"

#ifdef __ANDROID__
#ifndef HAVE_CLOSE_RANGE
static int close_range(unsigned int first, unsigned int last, unsigned int flags) {
int ret = 0;
for (unsigned int fd = first; fd <= last; fd++) {
if (close(fd) < 0)
ret = -1;
}
return ret;
}
#endif
#endif

#define MAX_MEM_PREALLOC_THREAD_COUNT 16

struct MemsetThread;
Expand Down Expand Up @@ -975,7 +988,11 @@ int qemu_shm_alloc(size_t size, Error **errp)
g_string_printf(shm_name, "/qemu-" FMT_pid "-shm-%d", getpid(),
cur_sequence);

#ifdef __ANDROID__
fd = open(shm_name->str, oflag, mode);
#else
fd = shm_open(shm_name->str, oflag, mode);
#endif
if (fd < 0) {
error_setg_errno(errp, errno,
"failed to create POSIX shared memory");
Expand All @@ -987,7 +1004,11 @@ int qemu_shm_alloc(size_t size, Error **errp)
* POSIX shared memory object. However it will remain allocated as long as
* there are file descriptors pointing to it.
*/
#ifdef __ANDROID__
unlink(shm_name->str);
#else
shm_unlink(shm_name->str);
#endif

if (ftruncate(fd, size) == -1) {
error_setg_errno(errp, errno,
Expand Down