3092
|
1 /* Virtual diry bit implementation (platform independent) for XEmacs.
|
|
2 Copyright (C) 2005 Marcus Crestani.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Not in FSF. */
|
|
22
|
|
23 #include <config.h>
|
|
24 #include "lisp.h"
|
|
25 #include "gc.h"
|
|
26 #include "mc-alloc.h"
|
|
27 #include "vdb.h"
|
|
28
|
|
29
|
|
30 typedef struct
|
|
31 {
|
|
32 Dynarr_declare (void *);
|
|
33 } void_ptr_dynarr;
|
|
34
|
|
35 void_ptr_dynarr *page_fault_table;
|
|
36
|
|
37 /* Init page fault table and protect heap. */
|
|
38 void
|
|
39 vdb_start_dirty_bits_recording (void)
|
|
40 {
|
3303
|
41 Elemcount protected_pages = (Elemcount) protect_heap_pages ();
|
3092
|
42 page_fault_table = Dynarr_new2 (void_ptr_dynarr, void *);
|
3303
|
43 Dynarr_resize (page_fault_table, protected_pages);
|
3092
|
44 }
|
|
45
|
|
46 /* Remove heap protection. */
|
|
47 void
|
|
48 vdb_stop_dirty_bits_recording (void)
|
|
49 {
|
|
50 unprotect_heap_pages ();
|
|
51 }
|
|
52
|
|
53 /* Read page fault table and pass page faults to garbage collector. */
|
|
54 int
|
|
55 vdb_read_dirty_bits (void)
|
|
56 {
|
|
57 int repushed_objects = 0;
|
|
58 Elemcount count;
|
|
59 for (count = Dynarr_length (page_fault_table); count; count--)
|
|
60 repushed_objects +=
|
|
61 repush_all_objects_on_page (Dynarr_at (page_fault_table, count - 1));
|
|
62 Dynarr_free (page_fault_table);
|
|
63 page_fault_table = 0;
|
|
64 return repushed_objects;
|
|
65 }
|
|
66
|
|
67 /* Called by the page fault handler: add address to page fault table. */
|
|
68 void
|
|
69 vdb_designate_modified (void *addr)
|
|
70 {
|
|
71 Dynarr_add (page_fault_table, addr);
|
|
72 }
|
|
73
|
|
74
|
|
75 /* For testing and debugging... */
|
|
76
|
|
77 DEFUN ("test-vdb", Ftest_vdb, 0, 0, "", /*
|
|
78 Test virtual dirty bit implementation. Prints results to stderr.
|
|
79 */
|
|
80 ())
|
|
81 {
|
|
82 Rawbyte *p;
|
|
83 char c;
|
|
84 Elemcount count;
|
|
85
|
|
86 /* Wrap up gc (if currently running). */
|
|
87 gc_full ();
|
|
88
|
|
89 /* Allocate a buffer; it will have the default
|
|
90 protection of PROT_READ|PROT_WRITE. */
|
|
91 p = (Rawbyte *) mc_alloc (mc_get_page_size());
|
|
92 set_lheader_implementation ((struct lrecord_header *) p, &lrecord_cons);
|
4123
|
93 fprintf (stderr, "Allocate p: [%p ... %p], length %d\n",
|
|
94 p, p + mc_get_page_size (), (int) mc_get_page_size ());
|
3092
|
95
|
|
96 /* Test read. */
|
|
97 fprintf (stderr, "Attempt to read p[666]... ");
|
|
98 c = p[666];
|
|
99 fprintf (stderr, "read ok.\n");
|
|
100
|
|
101 /* Test write. */
|
|
102 fprintf (stderr, "Attempt to write 42 to p[666]... ");
|
|
103 p[666] = 42;
|
|
104 fprintf (stderr, "write ok, p[666] = %d\n", p[666]);
|
|
105
|
|
106 /* Mark the buffer read-only and set environemnt for write-barrier. */
|
|
107 fprintf (stderr, "Write-protect the page.\n");
|
|
108 MARK_BLACK (p);
|
|
109 vdb_start_dirty_bits_recording ();
|
|
110 write_barrier_enabled = 1;
|
|
111
|
|
112 /* Test write-barrier read. */
|
|
113 fprintf (stderr, "Attempt to read p[666]... ");
|
|
114 c = p[666];
|
|
115 fprintf (stderr, "read ok.\n");
|
|
116
|
|
117 /* Test write-barrier write, program receives SIGSEGV. */
|
|
118 fprintf (stderr, "Attempt to write 23 to p[666]... ");
|
|
119 p[666] = 23;
|
|
120 fprintf (stderr, "Written p[666] = %d\n", p[666]);
|
|
121
|
|
122 /* Stop write-barrier mode. */
|
|
123 write_barrier_enabled = 0;
|
|
124 MARK_WHITE (p);
|
|
125 vdb_unprotect (p, mc_get_page_size ());
|
|
126 for (count = Dynarr_length (page_fault_table); count; count--)
|
|
127 if (Dynarr_at (page_fault_table, count - 1) == &p[666])
|
4123
|
128 fprintf (stderr, "VALID page fault at %p\n",
|
|
129 Dynarr_at (page_fault_table, count - 1));
|
3092
|
130 else
|
4123
|
131 fprintf (stderr, "WRONG page fault at %p\n",
|
|
132 Dynarr_at (page_fault_table, count - 1));
|
3092
|
133 Dynarr_free (page_fault_table);
|
|
134 return Qnil;
|
|
135 }
|
|
136
|
|
137 DEFUN ("test-segfault", Ftest_segfault, 0, 0, "", /*
|
|
138 Test virtual dirty bit implementation: provoke a segfault on purpose.
|
|
139 WARNING: this function causes a SEGFAULT on purpose and thus crashes
|
|
140 XEmacs! This is only used for debbugging, e.g. for testing how the
|
|
141 debugger behaves when XEmacs segfaults and the write barrier is
|
|
142 enabled.
|
|
143 */
|
|
144 ())
|
|
145 {
|
|
146 Rawbyte *q = 0;
|
|
147 q[0] = 23;
|
|
148 return Qnil;
|
|
149 }
|
|
150
|
|
151 void
|
|
152 syms_of_vdb (void)
|
|
153 {
|
|
154 DEFSUBR (Ftest_vdb);
|
|
155 DEFSUBR (Ftest_segfault);
|
|
156 }
|