M7350/kernel/arch/um/os-Linux/mem.c

206 lines
4.0 KiB
C
Raw Permalink Normal View History

2024-09-09 08:52:07 +00:00
/*
* Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
* Licensed under the GPL
*/
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
2024-09-09 08:57:42 +00:00
#include <sys/vfs.h>
#include <linux/magic.h>
#include <init.h>
#include <os.h>
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
/* Set by make_tempfile() during early boot. */
2024-09-09 08:52:07 +00:00
static char *tempdir = NULL;
2024-09-09 08:57:42 +00:00
/* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */
static int __init check_tmpfs(const char *dir)
2024-09-09 08:52:07 +00:00
{
2024-09-09 08:57:42 +00:00
struct statfs st;
printf("Checking if %s is on tmpfs...", dir);
if (statfs(dir, &st) < 0) {
printf("%s\n", strerror(errno));
} else if (st.f_type != TMPFS_MAGIC) {
printf("no\n");
} else {
printf("OK\n");
return 0;
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
return -1;
2024-09-09 08:52:07 +00:00
}
/*
2024-09-09 08:57:42 +00:00
* Choose the tempdir to use. We want something on tmpfs so that our memory is
* not subject to the host's vm.dirty_ratio. If a tempdir is specified in the
* environment, we use that even if it's not on tmpfs, but we warn the user.
* Otherwise, we try common tmpfs locations, and if no tmpfs directory is found
* then we fall back to /tmp.
2024-09-09 08:52:07 +00:00
*/
2024-09-09 08:57:42 +00:00
static char * __init choose_tempdir(void)
2024-09-09 08:52:07 +00:00
{
2024-09-09 08:57:42 +00:00
static const char * const vars[] = {
"TMPDIR",
"TMP",
"TEMP",
NULL
};
static const char fallback_dir[] = "/tmp";
static const char * const tmpfs_dirs[] = {
"/dev/shm",
fallback_dir,
NULL
};
int i;
const char *dir;
printf("Checking environment variables for a tempdir...");
for (i = 0; vars[i]; i++) {
dir = getenv(vars[i]);
if ((dir != NULL) && (*dir != '\0')) {
printf("%s\n", dir);
if (check_tmpfs(dir) >= 0)
goto done;
else
goto warn;
}
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
printf("none found\n");
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
for (i = 0; tmpfs_dirs[i]; i++) {
dir = tmpfs_dirs[i];
if (check_tmpfs(dir) >= 0)
goto done;
}
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
dir = fallback_dir;
warn:
printf("Warning: tempdir %s is not on tmpfs\n", dir);
done:
/* Make a copy since getenv results may not remain valid forever. */
return strdup(dir);
2024-09-09 08:52:07 +00:00
}
/*
2024-09-09 08:57:42 +00:00
* Create an unlinked tempfile in a suitable tempdir. template must be the
* basename part of the template with a leading '/'.
2024-09-09 08:52:07 +00:00
*/
2024-09-09 08:57:42 +00:00
static int __init make_tempfile(const char *template)
2024-09-09 08:52:07 +00:00
{
char *tempname;
int fd;
2024-09-09 08:57:42 +00:00
if (tempdir == NULL) {
tempdir = choose_tempdir();
if (tempdir == NULL) {
fprintf(stderr, "Failed to choose tempdir: %s\n",
strerror(errno));
return -1;
}
}
tempname = malloc(strlen(tempdir) + strlen(template) + 1);
2024-09-09 08:52:07 +00:00
if (tempname == NULL)
return -1;
2024-09-09 08:57:42 +00:00
strcpy(tempname, tempdir);
strcat(tempname, template);
2024-09-09 08:52:07 +00:00
fd = mkstemp(tempname);
if (fd < 0) {
fprintf(stderr, "open - cannot create %s: %s\n", tempname,
strerror(errno));
goto out;
}
2024-09-09 08:57:42 +00:00
if (unlink(tempname) < 0) {
2024-09-09 08:52:07 +00:00
perror("unlink");
goto close;
}
2024-09-09 08:57:42 +00:00
free(tempname);
2024-09-09 08:52:07 +00:00
return fd;
close:
close(fd);
out:
free(tempname);
return -1;
}
2024-09-09 08:57:42 +00:00
#define TEMPNAME_TEMPLATE "/vm_file-XXXXXX"
2024-09-09 08:52:07 +00:00
static int __init create_tmp_file(unsigned long long len)
{
int fd, err;
char zero;
2024-09-09 08:57:42 +00:00
fd = make_tempfile(TEMPNAME_TEMPLATE);
2024-09-09 08:52:07 +00:00
if (fd < 0)
exit(1);
err = fchmod(fd, 0777);
if (err < 0) {
perror("fchmod");
exit(1);
}
/*
* Seek to len - 1 because writing a character there will
* increase the file size by one byte, to the desired length.
*/
if (lseek64(fd, len - 1, SEEK_SET) < 0) {
perror("lseek64");
exit(1);
}
zero = 0;
err = write(fd, &zero, 1);
if (err != 1) {
perror("write");
exit(1);
}
return fd;
}
int __init create_mem_file(unsigned long long len)
{
int err, fd;
fd = create_tmp_file(len);
err = os_set_exec_close(fd);
if (err < 0) {
errno = -err;
perror("exec_close");
}
return fd;
}
void __init check_tmpexec(void)
{
void *addr;
int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
addr = mmap(NULL, UM_KERN_PAGE_SIZE,
PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
2024-09-09 08:57:42 +00:00
printf("Checking PROT_EXEC mmap in %s...", tempdir);
2024-09-09 08:52:07 +00:00
if (addr == MAP_FAILED) {
err = errno;
2024-09-09 08:57:42 +00:00
printf("%s\n", strerror(err));
2024-09-09 08:52:07 +00:00
close(fd);
if (err == EPERM)
2024-09-09 08:57:42 +00:00
printf("%s must be not mounted noexec\n", tempdir);
2024-09-09 08:52:07 +00:00
exit(1);
}
printf("OK\n");
munmap(addr, UM_KERN_PAGE_SIZE);
close(fd);
}