forked from free1139/ziron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabigen_generator.cpp
230 lines (180 loc) · 6.01 KB
/
abigen_generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include <ctime>
#include "generator.h"
#include "header_generator.h"
#include "vdso_wrapper_generator.h"
#include "abigen_generator.h"
using std::map;
using std::string;
using std::vector;
const map<string, string> user_attrs = {
{"noreturn", "__NO_RETURN"},
{"const", "__CONST"},
{"deprecated", "__DEPRECATE"},
// All vDSO calls are "leaf" in the sense of the GCC attribute.
// It just means they can't ever call back into their callers'
// own translation unit. No vDSO calls make callbacks at all.
{"*", "__LEAF_FN"},
};
const map<string, string> kernel_attrs = {
{"noreturn", "__NO_RETURN"},
};
static TestWrapper test_wrapper;
static BlockingRetryWrapper blocking_wrapper;
static vector<CallWrapper*> wrappers = {&test_wrapper, &blocking_wrapper};
static VdsoWrapperGenerator vdso_wrapper_generator(
"_zx_", // wrapper function name
"SYSCALL_zx_", // syscall implementation name
wrappers);
static KernelBranchGenerator kernel_branch;
static KernelWrapperGenerator kernel_wrappers(
"sys_", // function prefix
"wrapper_", // wrapper prefix
"ZX_SYS_"); // syscall numbers constant prefix
static bool skip_nothing(const Syscall&) {
return false;
}
static bool skip_internal(const Syscall& sc) {
return sc.is_internal();
}
static bool skip_vdso(const Syscall& sc) {
return sc.is_vdso();
}
static HeaderGenerator user_header(
"extern ", // function prefix
{
{"zx_", skip_internal},
{"_zx_", skip_internal},
},
"void", // no-args special type
false, // wrap pointers
user_attrs);
static HeaderGenerator vdso_header(
"__LOCAL extern ", // function prefix
{
{"VDSO_zx_", skip_nothing},
{"SYSCALL_zx_", skip_vdso},
},
"void", // no-args special type
false,
user_attrs);
static HeaderGenerator kernel_header(
"",
{
{"sys_", skip_vdso},
},
"",
true,
kernel_attrs);
static VDsoAsmGenerator vdso_asm_generator(
"m_syscall", // syscall macro name
"zx_", // syscall name prefix
wrappers);
static SyscallNumbersGenerator syscall_num_generator("#define ZX_SYS_");
static RustBindingGenerator rust_binding_generator;
static TraceInfoGenerator trace_generator;
static CategoryGenerator category_generator;
static JsonGenerator json_generator;
const map<string, Generator&> type_to_generator = {
// The user header, pure C.
{"user-header", user_header},
// The vDSO-internal header, pure C. (VDsoHeaderC)
{"vdso-header", vdso_header},
// The kernel header, C++.
{"kernel-header", kernel_header},
// The kernel assembly branches and jump table.
{"kernel-branch", kernel_branch},
// The kernel C++ wrappers.
{"kernel-wrappers", kernel_wrappers},
// The assembly file for x86-64.
{"x86-asm", vdso_asm_generator},
// The assembly include file for ARM64.
{"arm-asm", vdso_asm_generator},
// A C header defining ZX_SYS_* syscall number macros.
{"numbers", syscall_num_generator},
// The trace subsystem data, to be interpreted as an array of structs.
{"trace", trace_generator},
// Rust bindings.
{"rust", rust_binding_generator},
// vDSO wrappers for additional behaviour in user space.
{"vdso-wrappers", vdso_wrapper_generator},
// Category list.
{"category", category_generator},
// JSON list of syscalls.
{"json", json_generator},
};
const map<string, string> type_to_default_suffix = {
{"user-header", ".user.h"},
{"vdso-header", ".vdso.h"},
{"kernel-header", ".kernel.h"},
{"kernel-branch", ".kernel-branch.S"},
{"kernel-wrappers", ".kernel-wrappers.inc"},
{"x86-asm", ".x86-64.S"},
{"arm-asm", ".arm64.S"},
{"numbers", ".syscall-numbers.h"},
{"trace", ".trace.inc"},
{"rust", ".rs"},
{"vdso-wrappers", ".vdso-wrappers.inc"},
{"category", ".category.inc"},
{"json", ".json"},
};
const map<string, string>& get_type_to_default_suffix() {
return type_to_default_suffix;
}
const map<string, Generator&>& get_type_to_generator() {
return type_to_generator;
}
bool AbigenGenerator::AddSyscall(Syscall&& syscall) {
if (!syscall.validate())
return false;
syscall.requirements = pending_requirements_;
pending_requirements_.clear();
syscall.top_description = pending_top_description_;
pending_top_description_ = TopDescription();
syscall.assign_index(&next_index_);
calls_.emplace_back(std::move(syscall));
return true;
}
bool AbigenGenerator::Generate(const map<string, string>& type_to_filename) {
for (auto& entry : type_to_filename) {
if (!generate_one(entry.second, type_to_generator.at(entry.first), entry.first))
return false;
}
return true;
}
bool AbigenGenerator::verbose() const {
return verbose_;
}
void AbigenGenerator::AppendRequirement(Requirement&& req) {
pending_requirements_.emplace_back(req);
}
void AbigenGenerator::SetTopDescription(TopDescription&& td) {
pending_top_description_ = std::move(td);
}
bool AbigenGenerator::generate_one(
const string& output_file, Generator& generator, const string& type) {
std::ofstream ofile;
ofile.open(output_file.c_str(), std::ofstream::out);
if (!generator.header(ofile)) {
print_error("i/o error", output_file);
return false;
}
if (!std::all_of(calls_.begin(), calls_.end(),
[&generator, &ofile](const Syscall& sc) {
return generator.syscall(ofile, sc);
})) {
print_error("generation failed", output_file);
return false;
}
if (!generator.footer(ofile)) {
print_error("i/o error", output_file);
return false;
}
return true;
}
void AbigenGenerator::print_error(const char* what, const string& file) {
fprintf(stderr, "error: %s for %s\n", what, file.c_str());
}