| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define _GNU_SOURCE |
| 20 |
|
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/shm.h> |
| 23 |
#include <sys/wait.h> |
| 24 |
#include <sys/mman.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <hugetlbfs.h> |
| 34 |
#include "hugetests.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#define RANDOM_CONSTANT 0x1234ABCD |
| 48 |
#define OTHER_CONSTANT 0xfeef5678 |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static void sigchld_handler(int signum, siginfo_t *si, void *uc) |
| 54 |
{ |
| 55 |
if (WEXITSTATUS(si->si_status) != 0) |
| 56 |
FAIL("Child failed: %d", WEXITSTATUS(si->si_status)); |
| 57 |
if (WIFSIGNALED(si->si_status)) |
| 58 |
FAIL("Child recived signal %s", |
| 59 |
strsignal(WTERMSIG(si->si_status))); |
| 60 |
} |
| 61 |
|
| 62 |
int main(int argc, char ** argv) |
| 63 |
{ |
| 64 |
int fd, ret, status; |
| 65 |
void *syncarea; |
| 66 |
volatile unsigned int *p; |
| 67 |
volatile unsigned int *trigger, *child_readback; |
| 68 |
unsigned int parent_readback; |
| 69 |
long hpage_size; |
| 70 |
pid_t pid; |
| 71 |
struct sigaction sa = { |
| 72 |
.sa_sigaction = sigchld_handler, |
| 73 |
.sa_flags = SA_SIGINFO, |
| 74 |
}; |
| 75 |
|
| 76 |
test_init(argc, argv); |
| 77 |
|
| 78 |
check_free_huge_pages(2); |
| 79 |
|
| 80 |
if (argc != 1) |
| 81 |
CONFIG("Usage: fork-cow\n"); |
| 82 |
|
| 83 |
|
| 84 |
verbose_printf("Mapping synchronization area.."); |
| 85 |
syncarea = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, |
| 86 |
MAP_SHARED|MAP_ANONYMOUS, -1, 0); |
| 87 |
if (syncarea == MAP_FAILED) |
| 88 |
FAIL("mmap() sync area: %s", strerror(errno)); |
| 89 |
verbose_printf("done\n"); |
| 90 |
|
| 91 |
trigger = syncarea; |
| 92 |
*trigger = 0; |
| 93 |
|
| 94 |
child_readback = trigger + 1; |
| 95 |
*child_readback = 0; |
| 96 |
|
| 97 |
hpage_size = check_hugepagesize(); |
| 98 |
|
| 99 |
fd = hugetlbfs_unlinked_fd(); |
| 100 |
if (fd < 0) |
| 101 |
CONFIG("hugetlbfs_unlinked_fd() failed\n"); |
| 102 |
|
| 103 |
verbose_printf("Mapping hugepage area..."); |
| 104 |
p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 105 |
if (p == MAP_FAILED) |
| 106 |
FAIL("mmap(): %s", strerror(errno)); |
| 107 |
verbose_printf("mapped at %p\n", p); |
| 108 |
|
| 109 |
|
| 110 |
verbose_printf("Parent writes pre-fork..."); |
| 111 |
*p = RANDOM_CONSTANT; |
| 112 |
verbose_printf("%x\n", RANDOM_CONSTANT); |
| 113 |
|
| 114 |
ret = sigaction(SIGCHLD, &sa, NULL); |
| 115 |
if (ret) |
| 116 |
FAIL("sigaction(): %s", strerror(errno)); |
| 117 |
|
| 118 |
if ((pid = fork()) < 0) |
| 119 |
FAIL("fork(): %s", strerror(errno)); |
| 120 |
|
| 121 |
if (pid != 0) { |
| 122 |
|
| 123 |
verbose_printf("Parent writes post-fork..."); |
| 124 |
*p = ~RANDOM_CONSTANT; |
| 125 |
verbose_printf("%x\n", ~RANDOM_CONSTANT); |
| 126 |
|
| 127 |
*trigger = 1; |
| 128 |
|
| 129 |
while (*trigger != 2) |
| 130 |
; |
| 131 |
|
| 132 |
verbose_printf("Parent reads.."); |
| 133 |
parent_readback = *p; |
| 134 |
verbose_printf("%x\n", parent_readback); |
| 135 |
|
| 136 |
*trigger = 3; |
| 137 |
} else { |
| 138 |
|
| 139 |
verbose_printf("Child starts..\n"); |
| 140 |
|
| 141 |
while (*trigger != 1) |
| 142 |
; |
| 143 |
|
| 144 |
verbose_printf("Child reads..."); |
| 145 |
*child_readback = *p; |
| 146 |
verbose_printf("%x\n", *child_readback); |
| 147 |
|
| 148 |
verbose_printf("Child writes..."); |
| 149 |
*p = OTHER_CONSTANT; |
| 150 |
verbose_printf("%x\n", OTHER_CONSTANT); |
| 151 |
|
| 152 |
*trigger = 2; |
| 153 |
|
| 154 |
while (*trigger != 3) |
| 155 |
; |
| 156 |
|
| 157 |
verbose_printf("Child exits...\n"); |
| 158 |
exit(0); |
| 159 |
} |
| 160 |
|
| 161 |
verbose_printf("child_readback = 0x%x, parent_readback = 0x%x\n", |
| 162 |
*child_readback, parent_readback); |
| 163 |
|
| 164 |
if (*child_readback != RANDOM_CONSTANT) |
| 165 |
FAIL("Child read back 0x%x instead of 0x%x", |
| 166 |
*child_readback, RANDOM_CONSTANT); |
| 167 |
if (parent_readback != ~RANDOM_CONSTANT) |
| 168 |
FAIL("Parent read back 0x%x instead of 0x%x", |
| 169 |
parent_readback, RANDOM_CONSTANT); |
| 170 |
|
| 171 |
ret = waitpid(pid, &status, 0); |
| 172 |
if (ret < 0) |
| 173 |
FAIL("waitpid(): %s", strerror(errno)); |
| 174 |
|
| 175 |
PASS(); |
| 176 |
} |