Nigel Tao | fc66207 | 2019-02-02 08:01:01 +1100 | [diff] [blame] | 1 | // Copyright 2019 The Wuffs Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // ---------------- |
| 16 | |
| 17 | // This program demonstrates mmap'ing a ring-buffer's N bytes of physical |
| 18 | // memory three times, to be a contiguous block of 3*N bytes. The three |
| 19 | // pointers (base + 0*N + i), (base + 1*N + i) and (base + 2*N + i), which are |
| 20 | // different addresses in virtual memory, all alias the same physical address. |
| 21 | // |
| 22 | // Reading or writing a chunk of length M <= N is therefore a simple memcpy, |
| 23 | // without having to explicitly wrap around the ring-buffer boundaries. |
| 24 | // |
| 25 | // This is similar to the technique discussed in |
| 26 | // https://lo.calho.st/quick-hacks/employing-black-magic-in-the-linux-page-table/ |
| 27 | // |
| 28 | // This program differs from that web page's discussion by mapping the physical |
| 29 | // memory three times, not just two. This lets us read or write, implicitly |
| 30 | // wrapping, both forwards (after the middle mapping's end) and backwards |
| 31 | // (before the middle mapping's start). That web page only considers forwards |
| 32 | // reads or writes. Backwards reads are useful when decoding a Lempel-Ziv style |
| 33 | // compression format, copying from history (recently decoded bytes). |
| 34 | // |
| 35 | // Its output should be: |
| 36 | // |
| 37 | // middle[-8] == 0x00 == 0x00 == middle[131064] |
| 38 | // middle[-7] == 0x00 == 0x00 == middle[131065] |
| 39 | // middle[-6] == 0x00 == 0x00 == middle[131066] |
| 40 | // middle[-5] == 0x00 == 0x00 == middle[131067] |
| 41 | // middle[-4] == 0x00 == 0x00 == middle[131068] |
| 42 | // middle[-3] == 0x00 == 0x00 == middle[131069] |
| 43 | // middle[-2] == 0x20 == 0x20 == middle[131070] |
| 44 | // middle[-1] == 0x21 == 0x21 == middle[131071] |
| 45 | // middle[ 0] == 0x22 == 0x22 == middle[131072] |
| 46 | // middle[ 1] == 0x23 == 0x23 == middle[131073] |
| 47 | // middle[ 2] == 0x12 == 0x12 == middle[131074] |
| 48 | // middle[ 3] == 0x13 == 0x13 == middle[131075] |
| 49 | // middle[ 4] == 0x30 == 0x30 == middle[131076] |
| 50 | // middle[ 5] == 0x31 == 0x31 == middle[131077] |
| 51 | // middle[ 6] == 0x32 == 0x32 == middle[131078] |
| 52 | // middle[ 7] == 0x17 == 0x17 == middle[131079] |
| 53 | |
| 54 | #include <stdint.h> |
| 55 | #include <stdio.h> |
| 56 | #include <string.h> |
| 57 | #include <sys/mman.h> |
Nigel Tao | 6729715 | 2019-02-02 17:09:14 +1100 | [diff] [blame] | 58 | #include <sys/syscall.h> |
Nigel Tao | fc66207 | 2019-02-02 08:01:01 +1100 | [diff] [blame] | 59 | #include <unistd.h> |
| 60 | |
| 61 | // We should be able to do: |
| 62 | // |
| 63 | // #include <sys/memfd.h> |
| 64 | // |
Nigel Tao | 6729715 | 2019-02-02 17:09:14 +1100 | [diff] [blame] | 65 | // to get the memfd_create function signature, but memfd_create is relatively |
| 66 | // recent. For some reason, this #include hits "No such file or directory" on |
| 67 | // Ubuntu 18.04 (linux 4.15, glibc 2.27), and there's also been problems on |
| 68 | // Debian systems. Instead, we explicitly define our own memfd_create. |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 69 | static int // |
| 70 | my_memfd_create(const char* name, unsigned int flags) { |
Nigel Tao | 6729715 | 2019-02-02 17:09:14 +1100 | [diff] [blame] | 71 | return syscall(__NR_memfd_create, name, flags); |
| 72 | } |
Nigel Tao | fc66207 | 2019-02-02 08:01:01 +1100 | [diff] [blame] | 73 | |
| 74 | #define N (128 * 1024) |
| 75 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 76 | void* // |
| 77 | make_ring_buffer() { |
Nigel Tao | fc66207 | 2019-02-02 08:01:01 +1100 | [diff] [blame] | 78 | int page_size = getpagesize(); |
| 79 | if ((N < page_size) || (page_size <= 0) || ((N % page_size) != 0)) { |
| 80 | return NULL; |
| 81 | } |
| 82 | |
Nigel Tao | 6729715 | 2019-02-02 17:09:14 +1100 | [diff] [blame] | 83 | int memfd = my_memfd_create("ring", 0); |
Nigel Tao | fc66207 | 2019-02-02 08:01:01 +1100 | [diff] [blame] | 84 | if (memfd == -1) { |
| 85 | return NULL; |
| 86 | } |
| 87 | if (ftruncate(memfd, N) == -1) { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | // Have the kernel find a contiguous range of unused address space. |
| 92 | void* base = mmap(NULL, 3 * N, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
| 93 | if (base == MAP_FAILED) { |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | // Map that "ring" file 3 times, filling that range exactly. |
| 98 | for (int i = 0; i < 3; i++) { |
| 99 | void* p = mmap(base + (i * N), N, PROT_READ | PROT_WRITE, |
| 100 | MAP_FIXED | MAP_SHARED, memfd, 0); |
| 101 | if (p == MAP_FAILED) { |
| 102 | return NULL; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | close(memfd); |
| 107 | return base; |
| 108 | } |
| 109 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 110 | int // |
| 111 | main(int argc, char** argv) { |
Nigel Tao | fc66207 | 2019-02-02 08:01:01 +1100 | [diff] [blame] | 112 | uint8_t* base = make_ring_buffer(); |
| 113 | if (!base) { |
| 114 | fprintf(stderr, "could not make ring buffer\n"); |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | for (int i = 0; i < 8; i++) { |
| 119 | base[i] = 0x10 + i; |
| 120 | } |
| 121 | |
| 122 | memcpy(base + N - 2, "\x20\x21\x22\x23", 4); |
| 123 | |
| 124 | base[(0 * N) + 4] = 0x30; |
| 125 | base[(1 * N) + 5] = 0x31; |
| 126 | base[(2 * N) + 6] = 0x32; |
| 127 | |
| 128 | uint8_t* middle = base + N; |
| 129 | for (int i = -8; i < 8; i++) { |
| 130 | int j = N + i; |
| 131 | printf("middle[%2d] == 0x%02X == 0x%02X == middle[%6d]\n", i, |
| 132 | middle[i], middle[j], j); |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |