]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/dcache-line-read-error.c
57db30c313fce133a5cec8fd3d0d72170821f490
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / dcache-line-read-error.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2012-2019 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <sys/mman.h>
21 #include <unistd.h>
22 #include <string.h>
23
24 size_t pg_size;
25 void *first_mapped_page;
26 void *last_mapped_page;
27
28 void
29 breakpt (void)
30 {
31 /* Nothing. */
32 }
33
34 int
35 main (void)
36 {
37 void *p;
38 int pg_count;
39 size_t i;
40
41 /* Map 6 contiguous pages, and then unmap all second first, and
42 second last.
43
44 From GDB we will disassemble each of the _mapped_ pages, with a
45 code-cache (dcache) line size bigger than the page size (twice
46 bigger). This makes GDB try to read one page before the mapped
47 page once, and the page after another time. GDB should give no
48 error in either case.
49
50 That is, depending on where the kernel aligns the pages, we get
51 either:
52
53 .---.---.---.---.---.---.
54 | U | M | U | U | M | U |
55 '---'---'---'---'---'---.
56 | | | | <- line alignment
57 ^^^^^^^ ^^^^^^^
58 | |
59 + line1 + line2
60
61 Or:
62
63 .---.---.---.---.---.---.
64 | U | M | U | U | M | U |
65 '---'---'---'---'---'---.
66 | | | <- line alignment
67 ^^^^^^^ ^^^^^^^
68 | |
69 line1 + + line2
70
71 Note we really want to test that dcache behaves correctly when
72 reading a cache line fails. We're just using unmapped memory as
73 proxy for any kind of error. */
74
75 pg_size = getpagesize ();
76 pg_count = 6;
77
78 p = mmap (0, pg_count * pg_size, PROT_READ|PROT_WRITE,
79 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
80 if (p == MAP_FAILED)
81 {
82 perror ("mmap");
83 return EXIT_FAILURE;
84 }
85
86 /* Leave memory zero-initialized. Disassembling 0s should behave on
87 all targets. */
88
89 for (i = 0; i < pg_count; i++)
90 {
91 if (i == 1 || i == 4)
92 continue;
93
94 if (munmap (p + (i * pg_size), pg_size) == -1)
95 {
96 perror ("munmap");
97 return EXIT_FAILURE;
98 }
99 }
100
101 first_mapped_page = p + 1 * pg_size;;
102 last_mapped_page = p + 4 * pg_size;
103
104 breakpt ();
105
106 return EXIT_SUCCESS;
107 }