• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
58 #include <sys/syscall.h>
59 #include <unistd.h>
60 
61 // We should be able to do:
62 //
63 // #include <sys/memfd.h>
64 //
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.
my_memfd_create(const char * name,unsigned int flags)69 static int my_memfd_create(const char* name, unsigned int flags) {
70   return syscall(__NR_memfd_create, name, flags);
71 }
72 
73 #define N (128 * 1024)
74 
make_ring_buffer()75 void* make_ring_buffer() {
76   int page_size = getpagesize();
77   if ((N < page_size) || (page_size <= 0) || ((N % page_size) != 0)) {
78     return NULL;
79   }
80 
81   int memfd = my_memfd_create("ring", 0);
82   if (memfd == -1) {
83     return NULL;
84   }
85   if (ftruncate(memfd, N) == -1) {
86     return NULL;
87   }
88 
89   // Have the kernel find a contiguous range of unused address space.
90   void* base = mmap(NULL, 3 * N, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
91   if (base == MAP_FAILED) {
92     return NULL;
93   }
94 
95   // Map that "ring" file 3 times, filling that range exactly.
96   for (int i = 0; i < 3; i++) {
97     void* p = mmap(base + (i * N), N, PROT_READ | PROT_WRITE,
98                    MAP_FIXED | MAP_SHARED, memfd, 0);
99     if (p == MAP_FAILED) {
100       return NULL;
101     }
102   }
103 
104   close(memfd);
105   return base;
106 }
107 
main(int argc,char ** argv)108 int main(int argc, char** argv) {
109   uint8_t* base = make_ring_buffer();
110   if (!base) {
111     fprintf(stderr, "could not make ring buffer\n");
112     return 1;
113   }
114 
115   for (int i = 0; i < 8; i++) {
116     base[i] = 0x10 + i;
117   }
118 
119   memcpy(base + N - 2, "\x20\x21\x22\x23", 4);
120 
121   base[(0 * N) + 4] = 0x30;
122   base[(1 * N) + 5] = 0x31;
123   base[(2 * N) + 6] = 0x32;
124 
125   uint8_t* middle = base + N;
126   for (int i = -8; i < 8; i++) {
127     int j = N + i;
128     printf("middle[%2d]  ==  0x%02X  ==  0x%02X  ==  middle[%6d]\n", i,
129            middle[i], middle[j], j);
130   }
131 
132   return 0;
133 }
134