x86: pcihp: acpi: prepare slot ignore rule to work with self describing bridges
[qemu.git] / scripts / block-coroutine-wrapper.py
blobdff3af49f5805eb4d9406dcc53ca5e101a6cef55
1 #! /usr/bin/env python3
2 """Generate coroutine wrappers for block subsystem.
4 The program parses one or several concatenated c files from stdin,
5 searches for functions with the 'co_wrapper' specifier
6 and generates corresponding wrappers on stdout.
8 Usage: block-coroutine-wrapper.py generated-file.c FILE.[ch]...
10 Copyright (c) 2020 Virtuozzo International GmbH.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 """
26 import sys
27 import re
28 from typing import Iterator
31 def gen_header():
32 copyright = re.sub('^.*Copyright', 'Copyright', __doc__, flags=re.DOTALL)
33 copyright = re.sub('^(?=.)', ' * ', copyright.strip(), flags=re.MULTILINE)
34 copyright = re.sub('^$', ' *', copyright, flags=re.MULTILINE)
35 return f"""\
37 * File is generated by scripts/block-coroutine-wrapper.py
39 {copyright}
42 #include "qemu/osdep.h"
43 #include "block/coroutines.h"
44 #include "block/block-gen.h"
45 #include "block/block_int.h"
46 #include "block/dirty-bitmap.h"
47 """
50 class ParamDecl:
51 param_re = re.compile(r'(?P<decl>'
52 r'(?P<type>.*[ *])'
53 r'(?P<name>[a-z][a-z0-9_]*)'
54 r')')
56 def __init__(self, param_decl: str) -> None:
57 m = self.param_re.match(param_decl.strip())
58 if m is None:
59 raise ValueError(f'Wrong parameter declaration: "{param_decl}"')
60 self.decl = m.group('decl')
61 self.type = m.group('type')
62 self.name = m.group('name')
65 class FuncDecl:
66 def __init__(self, return_type: str, name: str, args: str,
67 variant: str) -> None:
68 self.return_type = return_type.strip()
69 self.name = name.strip()
70 self.struct_name = snake_to_camel(self.name)
71 self.args = [ParamDecl(arg.strip()) for arg in args.split(',')]
72 self.create_only_co = 'mixed' not in variant
73 self.graph_rdlock = 'bdrv_rdlock' in variant
75 subsystem, subname = self.name.split('_', 1)
76 self.co_name = f'{subsystem}_co_{subname}'
78 t = self.args[0].type
79 if t == 'BlockDriverState *':
80 ctx = 'bdrv_get_aio_context(bs)'
81 elif t == 'BdrvChild *':
82 ctx = 'bdrv_get_aio_context(child->bs)'
83 elif t == 'BlockBackend *':
84 ctx = 'blk_get_aio_context(blk)'
85 else:
86 ctx = 'qemu_get_aio_context()'
87 self.ctx = ctx
89 def gen_list(self, format: str) -> str:
90 return ', '.join(format.format_map(arg.__dict__) for arg in self.args)
92 def gen_block(self, format: str) -> str:
93 return '\n'.join(format.format_map(arg.__dict__) for arg in self.args)
96 # Match wrappers declared with a co_wrapper mark
97 func_decl_re = re.compile(r'^(?P<return_type>[a-zA-Z][a-zA-Z0-9_]* [\*]?)'
98 r'\s*co_wrapper'
99 r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*'
100 r'(?P<wrapper_name>[a-z][a-z0-9_]*)'
101 r'\((?P<args>[^)]*)\);$', re.MULTILINE)
104 def func_decl_iter(text: str) -> Iterator:
105 for m in func_decl_re.finditer(text):
106 yield FuncDecl(return_type=m.group('return_type'),
107 name=m.group('wrapper_name'),
108 args=m.group('args'),
109 variant=m.group('variant'))
112 def snake_to_camel(func_name: str) -> str:
114 Convert underscore names like 'some_function_name' to camel-case like
115 'SomeFunctionName'
117 words = func_name.split('_')
118 words = [w[0].upper() + w[1:] for w in words]
119 return ''.join(words)
122 def create_mixed_wrapper(func: FuncDecl) -> str:
124 Checks if we are already in coroutine
126 name = func.co_name
127 struct_name = func.struct_name
128 graph_assume_lock = 'assume_graph_lock();' if func.graph_rdlock else ''
130 return f"""\
131 {func.return_type} {func.name}({ func.gen_list('{decl}') })
133 if (qemu_in_coroutine()) {{
134 {graph_assume_lock}
135 return {name}({ func.gen_list('{name}') });
136 }} else {{
137 {struct_name} s = {{
138 .poll_state.ctx = {func.ctx},
139 .poll_state.in_progress = true,
141 { func.gen_block(' .{name} = {name},') }
144 s.poll_state.co = qemu_coroutine_create({name}_entry, &s);
146 bdrv_poll_co(&s.poll_state);
147 return s.ret;
149 }}"""
152 def create_co_wrapper(func: FuncDecl) -> str:
154 Assumes we are not in coroutine, and creates one
156 name = func.co_name
157 struct_name = func.struct_name
158 return f"""\
159 {func.return_type} {func.name}({ func.gen_list('{decl}') })
161 {struct_name} s = {{
162 .poll_state.ctx = {func.ctx},
163 .poll_state.in_progress = true,
165 { func.gen_block(' .{name} = {name},') }
167 assert(!qemu_in_coroutine());
169 s.poll_state.co = qemu_coroutine_create({name}_entry, &s);
171 bdrv_poll_co(&s.poll_state);
172 return s.ret;
173 }}"""
176 def gen_wrapper(func: FuncDecl) -> str:
177 assert not '_co_' in func.name
179 name = func.co_name
180 struct_name = func.struct_name
182 graph_lock=''
183 graph_unlock=''
184 if func.graph_rdlock:
185 graph_lock=' bdrv_graph_co_rdlock();'
186 graph_unlock=' bdrv_graph_co_rdunlock();'
188 creation_function = create_mixed_wrapper
189 if func.create_only_co:
190 creation_function = create_co_wrapper
192 return f"""\
194 * Wrappers for {name}
197 typedef struct {struct_name} {{
198 BdrvPollCo poll_state;
199 {func.return_type} ret;
200 { func.gen_block(' {decl};') }
201 }} {struct_name};
203 static void coroutine_fn {name}_entry(void *opaque)
205 {struct_name} *s = opaque;
207 {graph_lock}
208 s->ret = {name}({ func.gen_list('s->{name}') });
209 {graph_unlock}
210 s->poll_state.in_progress = false;
212 aio_wait_kick();
215 {creation_function(func)}"""
218 def gen_wrappers(input_code: str) -> str:
219 res = ''
220 for func in func_decl_iter(input_code):
221 res += '\n\n\n'
222 res += gen_wrapper(func)
224 return res
227 if __name__ == '__main__':
228 if len(sys.argv) < 3:
229 exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...')
231 with open(sys.argv[1], 'w', encoding='utf-8') as f_out:
232 f_out.write(gen_header())
233 for fname in sys.argv[2:]:
234 with open(fname, encoding='utf-8') as f_in:
235 f_out.write(gen_wrappers(f_in.read()))
236 f_out.write('\n')