xref: /openbmc/qemu/scripts/block-coroutine-wrapper.py (revision e6d3f7a602a370362bc52b0aed7dfff1a0bf726d)
1aaaa20b6SVladimir Sementsov-Ogievskiy#! /usr/bin/env python3
2aaaa20b6SVladimir Sementsov-Ogievskiy"""Generate coroutine wrappers for block subsystem.
3aaaa20b6SVladimir Sementsov-Ogievskiy
4aaaa20b6SVladimir Sementsov-OgievskiyThe program parses one or several concatenated c files from stdin,
576a2f554SEmanuele Giuseppe Espositosearches for functions with the 'co_wrapper' specifier
6aaaa20b6SVladimir Sementsov-Ogievskiyand generates corresponding wrappers on stdout.
7aaaa20b6SVladimir Sementsov-Ogievskiy
8aaaa20b6SVladimir Sementsov-OgievskiyUsage: block-coroutine-wrapper.py generated-file.c FILE.[ch]...
9aaaa20b6SVladimir Sementsov-Ogievskiy
10aaaa20b6SVladimir Sementsov-OgievskiyCopyright (c) 2020 Virtuozzo International GmbH.
11aaaa20b6SVladimir Sementsov-Ogievskiy
12aaaa20b6SVladimir Sementsov-OgievskiyThis program is free software; you can redistribute it and/or modify
13aaaa20b6SVladimir Sementsov-Ogievskiyit under the terms of the GNU General Public License as published by
14aaaa20b6SVladimir Sementsov-Ogievskiythe Free Software Foundation; either version 2 of the License, or
15aaaa20b6SVladimir Sementsov-Ogievskiy(at your option) any later version.
16aaaa20b6SVladimir Sementsov-Ogievskiy
17aaaa20b6SVladimir Sementsov-OgievskiyThis program is distributed in the hope that it will be useful,
18aaaa20b6SVladimir Sementsov-Ogievskiybut WITHOUT ANY WARRANTY; without even the implied warranty of
19aaaa20b6SVladimir Sementsov-OgievskiyMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20aaaa20b6SVladimir Sementsov-OgievskiyGNU General Public License for more details.
21aaaa20b6SVladimir Sementsov-Ogievskiy
22aaaa20b6SVladimir Sementsov-OgievskiyYou should have received a copy of the GNU General Public License
23aaaa20b6SVladimir Sementsov-Ogievskiyalong with this program.  If not, see <http://www.gnu.org/licenses/>.
24aaaa20b6SVladimir Sementsov-Ogievskiy"""
25aaaa20b6SVladimir Sementsov-Ogievskiy
26aaaa20b6SVladimir Sementsov-Ogievskiyimport sys
27aaaa20b6SVladimir Sementsov-Ogievskiyimport re
28aaaa20b6SVladimir Sementsov-Ogievskiyfrom typing import Iterator
29aaaa20b6SVladimir Sementsov-Ogievskiy
30aaaa20b6SVladimir Sementsov-Ogievskiy
31aaaa20b6SVladimir Sementsov-Ogievskiydef gen_header():
32aaaa20b6SVladimir Sementsov-Ogievskiy    copyright = re.sub('^.*Copyright', 'Copyright', __doc__, flags=re.DOTALL)
33aaaa20b6SVladimir Sementsov-Ogievskiy    copyright = re.sub('^(?=.)', ' * ', copyright.strip(), flags=re.MULTILINE)
34aaaa20b6SVladimir Sementsov-Ogievskiy    copyright = re.sub('^$', ' *', copyright, flags=re.MULTILINE)
35aaaa20b6SVladimir Sementsov-Ogievskiy    return f"""\
36aaaa20b6SVladimir Sementsov-Ogievskiy/*
37aaaa20b6SVladimir Sementsov-Ogievskiy * File is generated by scripts/block-coroutine-wrapper.py
38aaaa20b6SVladimir Sementsov-Ogievskiy *
39aaaa20b6SVladimir Sementsov-Ogievskiy{copyright}
40aaaa20b6SVladimir Sementsov-Ogievskiy */
41aaaa20b6SVladimir Sementsov-Ogievskiy
42aaaa20b6SVladimir Sementsov-Ogievskiy#include "qemu/osdep.h"
43aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/coroutines.h"
44aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/block-gen.h"
45aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/block_int.h"\
46aaaa20b6SVladimir Sementsov-Ogievskiy"""
47aaaa20b6SVladimir Sementsov-Ogievskiy
48aaaa20b6SVladimir Sementsov-Ogievskiy
49aaaa20b6SVladimir Sementsov-Ogievskiyclass ParamDecl:
50aaaa20b6SVladimir Sementsov-Ogievskiy    param_re = re.compile(r'(?P<decl>'
51aaaa20b6SVladimir Sementsov-Ogievskiy                          r'(?P<type>.*[ *])'
52aaaa20b6SVladimir Sementsov-Ogievskiy                          r'(?P<name>[a-z][a-z0-9_]*)'
53aaaa20b6SVladimir Sementsov-Ogievskiy                          r')')
54aaaa20b6SVladimir Sementsov-Ogievskiy
55aaaa20b6SVladimir Sementsov-Ogievskiy    def __init__(self, param_decl: str) -> None:
56aaaa20b6SVladimir Sementsov-Ogievskiy        m = self.param_re.match(param_decl.strip())
57aaaa20b6SVladimir Sementsov-Ogievskiy        if m is None:
58aaaa20b6SVladimir Sementsov-Ogievskiy            raise ValueError(f'Wrong parameter declaration: "{param_decl}"')
59aaaa20b6SVladimir Sementsov-Ogievskiy        self.decl = m.group('decl')
60aaaa20b6SVladimir Sementsov-Ogievskiy        self.type = m.group('type')
61aaaa20b6SVladimir Sementsov-Ogievskiy        self.name = m.group('name')
62aaaa20b6SVladimir Sementsov-Ogievskiy
63aaaa20b6SVladimir Sementsov-Ogievskiy
64aaaa20b6SVladimir Sementsov-Ogievskiyclass FuncDecl:
6576a2f554SEmanuele Giuseppe Esposito    def __init__(self, return_type: str, name: str, args: str,
6676a2f554SEmanuele Giuseppe Esposito                 variant: str) -> None:
67aaaa20b6SVladimir Sementsov-Ogievskiy        self.return_type = return_type.strip()
68aaaa20b6SVladimir Sementsov-Ogievskiy        self.name = name.strip()
6976a2f554SEmanuele Giuseppe Esposito        self.struct_name = snake_to_camel(self.name)
70aaaa20b6SVladimir Sementsov-Ogievskiy        self.args = [ParamDecl(arg.strip()) for arg in args.split(',')]
7176a2f554SEmanuele Giuseppe Esposito        self.create_only_co = 'mixed' not in variant
72*e6d3f7a6SEmanuele Giuseppe Esposito        self.graph_rdlock = 'bdrv_rdlock' in variant
7376a2f554SEmanuele Giuseppe Esposito
7476a2f554SEmanuele Giuseppe Esposito        subsystem, subname = self.name.split('_', 1)
7576a2f554SEmanuele Giuseppe Esposito        self.co_name = f'{subsystem}_co_{subname}'
7676a2f554SEmanuele Giuseppe Esposito
7776a2f554SEmanuele Giuseppe Esposito        t = self.args[0].type
7876a2f554SEmanuele Giuseppe Esposito        if t == 'BlockDriverState *':
790582fb82SEmanuele Giuseppe Esposito            ctx = 'bdrv_get_aio_context(bs)'
8076a2f554SEmanuele Giuseppe Esposito        elif t == 'BdrvChild *':
810582fb82SEmanuele Giuseppe Esposito            ctx = 'bdrv_get_aio_context(child->bs)'
820582fb82SEmanuele Giuseppe Esposito        elif t == 'BlockBackend *':
830582fb82SEmanuele Giuseppe Esposito            ctx = 'blk_get_aio_context(blk)'
8476a2f554SEmanuele Giuseppe Esposito        else:
850582fb82SEmanuele Giuseppe Esposito            ctx = 'qemu_get_aio_context()'
860582fb82SEmanuele Giuseppe Esposito        self.ctx = ctx
87aaaa20b6SVladimir Sementsov-Ogievskiy
88aaaa20b6SVladimir Sementsov-Ogievskiy    def gen_list(self, format: str) -> str:
89aaaa20b6SVladimir Sementsov-Ogievskiy        return ', '.join(format.format_map(arg.__dict__) for arg in self.args)
90aaaa20b6SVladimir Sementsov-Ogievskiy
91aaaa20b6SVladimir Sementsov-Ogievskiy    def gen_block(self, format: str) -> str:
92aaaa20b6SVladimir Sementsov-Ogievskiy        return '\n'.join(format.format_map(arg.__dict__) for arg in self.args)
93aaaa20b6SVladimir Sementsov-Ogievskiy
94aaaa20b6SVladimir Sementsov-Ogievskiy
9576a2f554SEmanuele Giuseppe Esposito# Match wrappers declared with a co_wrapper mark
966700dfb1SEmanuele Giuseppe Espositofunc_decl_re = re.compile(r'^(?P<return_type>[a-zA-Z][a-zA-Z0-9_]* [\*]?)'
976700dfb1SEmanuele Giuseppe Esposito                          r'\s*co_wrapper'
9876a2f554SEmanuele Giuseppe Esposito                          r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*'
99aaaa20b6SVladimir Sementsov-Ogievskiy                          r'(?P<wrapper_name>[a-z][a-z0-9_]*)'
100aaaa20b6SVladimir Sementsov-Ogievskiy                          r'\((?P<args>[^)]*)\);$', re.MULTILINE)
101aaaa20b6SVladimir Sementsov-Ogievskiy
102aaaa20b6SVladimir Sementsov-Ogievskiy
103aaaa20b6SVladimir Sementsov-Ogievskiydef func_decl_iter(text: str) -> Iterator:
104aaaa20b6SVladimir Sementsov-Ogievskiy    for m in func_decl_re.finditer(text):
1056700dfb1SEmanuele Giuseppe Esposito        yield FuncDecl(return_type=m.group('return_type'),
106aaaa20b6SVladimir Sementsov-Ogievskiy                       name=m.group('wrapper_name'),
10776a2f554SEmanuele Giuseppe Esposito                       args=m.group('args'),
10876a2f554SEmanuele Giuseppe Esposito                       variant=m.group('variant'))
109aaaa20b6SVladimir Sementsov-Ogievskiy
110aaaa20b6SVladimir Sementsov-Ogievskiy
111aaaa20b6SVladimir Sementsov-Ogievskiydef snake_to_camel(func_name: str) -> str:
112aaaa20b6SVladimir Sementsov-Ogievskiy    """
113aaaa20b6SVladimir Sementsov-Ogievskiy    Convert underscore names like 'some_function_name' to camel-case like
114aaaa20b6SVladimir Sementsov-Ogievskiy    'SomeFunctionName'
115aaaa20b6SVladimir Sementsov-Ogievskiy    """
116aaaa20b6SVladimir Sementsov-Ogievskiy    words = func_name.split('_')
117aaaa20b6SVladimir Sementsov-Ogievskiy    words = [w[0].upper() + w[1:] for w in words]
118aaaa20b6SVladimir Sementsov-Ogievskiy    return ''.join(words)
119aaaa20b6SVladimir Sementsov-Ogievskiy
120aaaa20b6SVladimir Sementsov-Ogievskiy
12176a2f554SEmanuele Giuseppe Espositodef create_mixed_wrapper(func: FuncDecl) -> str:
12276a2f554SEmanuele Giuseppe Esposito    """
12376a2f554SEmanuele Giuseppe Esposito    Checks if we are already in coroutine
12476a2f554SEmanuele Giuseppe Esposito    """
12576a2f554SEmanuele Giuseppe Esposito    name = func.co_name
12676a2f554SEmanuele Giuseppe Esposito    struct_name = func.struct_name
127*e6d3f7a6SEmanuele Giuseppe Esposito    graph_assume_lock = 'assume_graph_lock();' if func.graph_rdlock else ''
128*e6d3f7a6SEmanuele Giuseppe Esposito
12976a2f554SEmanuele Giuseppe Esposito    return f"""\
1306700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') })
13176a2f554SEmanuele Giuseppe Esposito{{
13276a2f554SEmanuele Giuseppe Esposito    if (qemu_in_coroutine()) {{
133*e6d3f7a6SEmanuele Giuseppe Esposito        {graph_assume_lock}
13476a2f554SEmanuele Giuseppe Esposito        return {name}({ func.gen_list('{name}') });
13576a2f554SEmanuele Giuseppe Esposito    }} else {{
13676a2f554SEmanuele Giuseppe Esposito        {struct_name} s = {{
1370582fb82SEmanuele Giuseppe Esposito            .poll_state.ctx = {func.ctx},
13876a2f554SEmanuele Giuseppe Esposito            .poll_state.in_progress = true,
13976a2f554SEmanuele Giuseppe Esposito
14076a2f554SEmanuele Giuseppe Esposito{ func.gen_block('            .{name} = {name},') }
14176a2f554SEmanuele Giuseppe Esposito        }};
14276a2f554SEmanuele Giuseppe Esposito
14376a2f554SEmanuele Giuseppe Esposito        s.poll_state.co = qemu_coroutine_create({name}_entry, &s);
14476a2f554SEmanuele Giuseppe Esposito
1456700dfb1SEmanuele Giuseppe Esposito        bdrv_poll_co(&s.poll_state);
1466700dfb1SEmanuele Giuseppe Esposito        return s.ret;
14776a2f554SEmanuele Giuseppe Esposito    }}
14876a2f554SEmanuele Giuseppe Esposito}}"""
14976a2f554SEmanuele Giuseppe Esposito
15076a2f554SEmanuele Giuseppe Esposito
15176a2f554SEmanuele Giuseppe Espositodef create_co_wrapper(func: FuncDecl) -> str:
15276a2f554SEmanuele Giuseppe Esposito    """
15376a2f554SEmanuele Giuseppe Esposito    Assumes we are not in coroutine, and creates one
15476a2f554SEmanuele Giuseppe Esposito    """
15576a2f554SEmanuele Giuseppe Esposito    name = func.co_name
15676a2f554SEmanuele Giuseppe Esposito    struct_name = func.struct_name
15776a2f554SEmanuele Giuseppe Esposito    return f"""\
1586700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') })
15976a2f554SEmanuele Giuseppe Esposito{{
16076a2f554SEmanuele Giuseppe Esposito    {struct_name} s = {{
1610582fb82SEmanuele Giuseppe Esposito        .poll_state.ctx = {func.ctx},
16276a2f554SEmanuele Giuseppe Esposito        .poll_state.in_progress = true,
16376a2f554SEmanuele Giuseppe Esposito
16476a2f554SEmanuele Giuseppe Esposito{ func.gen_block('        .{name} = {name},') }
16576a2f554SEmanuele Giuseppe Esposito    }};
16676a2f554SEmanuele Giuseppe Esposito    assert(!qemu_in_coroutine());
16776a2f554SEmanuele Giuseppe Esposito
16876a2f554SEmanuele Giuseppe Esposito    s.poll_state.co = qemu_coroutine_create({name}_entry, &s);
16976a2f554SEmanuele Giuseppe Esposito
1706700dfb1SEmanuele Giuseppe Esposito    bdrv_poll_co(&s.poll_state);
1716700dfb1SEmanuele Giuseppe Esposito    return s.ret;
17276a2f554SEmanuele Giuseppe Esposito}}"""
17376a2f554SEmanuele Giuseppe Esposito
17476a2f554SEmanuele Giuseppe Esposito
175aaaa20b6SVladimir Sementsov-Ogievskiydef gen_wrapper(func: FuncDecl) -> str:
176bb436948SVladimir Sementsov-Ogievskiy    assert not '_co_' in func.name
177aaaa20b6SVladimir Sementsov-Ogievskiy
17876a2f554SEmanuele Giuseppe Esposito    name = func.co_name
17976a2f554SEmanuele Giuseppe Esposito    struct_name = func.struct_name
180bb436948SVladimir Sementsov-Ogievskiy
181*e6d3f7a6SEmanuele Giuseppe Esposito    graph_lock=''
182*e6d3f7a6SEmanuele Giuseppe Esposito    graph_unlock=''
183*e6d3f7a6SEmanuele Giuseppe Esposito    if func.graph_rdlock:
184*e6d3f7a6SEmanuele Giuseppe Esposito        graph_lock='    bdrv_graph_co_rdlock();'
185*e6d3f7a6SEmanuele Giuseppe Esposito        graph_unlock='    bdrv_graph_co_rdunlock();'
186*e6d3f7a6SEmanuele Giuseppe Esposito
18776a2f554SEmanuele Giuseppe Esposito    creation_function = create_mixed_wrapper
18876a2f554SEmanuele Giuseppe Esposito    if func.create_only_co:
18976a2f554SEmanuele Giuseppe Esposito        creation_function = create_co_wrapper
190aaaa20b6SVladimir Sementsov-Ogievskiy
191aaaa20b6SVladimir Sementsov-Ogievskiy    return f"""\
192aaaa20b6SVladimir Sementsov-Ogievskiy/*
193aaaa20b6SVladimir Sementsov-Ogievskiy * Wrappers for {name}
194aaaa20b6SVladimir Sementsov-Ogievskiy */
195aaaa20b6SVladimir Sementsov-Ogievskiy
196aaaa20b6SVladimir Sementsov-Ogievskiytypedef struct {struct_name} {{
197aaaa20b6SVladimir Sementsov-Ogievskiy    BdrvPollCo poll_state;
1986700dfb1SEmanuele Giuseppe Esposito    {func.return_type} ret;
199aaaa20b6SVladimir Sementsov-Ogievskiy{ func.gen_block('    {decl};') }
200aaaa20b6SVladimir Sementsov-Ogievskiy}} {struct_name};
201aaaa20b6SVladimir Sementsov-Ogievskiy
202aaaa20b6SVladimir Sementsov-Ogievskiystatic void coroutine_fn {name}_entry(void *opaque)
203aaaa20b6SVladimir Sementsov-Ogievskiy{{
204aaaa20b6SVladimir Sementsov-Ogievskiy    {struct_name} *s = opaque;
205aaaa20b6SVladimir Sementsov-Ogievskiy
206*e6d3f7a6SEmanuele Giuseppe Esposito{graph_lock}
2076700dfb1SEmanuele Giuseppe Esposito    s->ret = {name}({ func.gen_list('s->{name}') });
208*e6d3f7a6SEmanuele Giuseppe Esposito{graph_unlock}
209aaaa20b6SVladimir Sementsov-Ogievskiy    s->poll_state.in_progress = false;
210aaaa20b6SVladimir Sementsov-Ogievskiy
211aaaa20b6SVladimir Sementsov-Ogievskiy    aio_wait_kick();
212aaaa20b6SVladimir Sementsov-Ogievskiy}}
213aaaa20b6SVladimir Sementsov-Ogievskiy
21476a2f554SEmanuele Giuseppe Esposito{creation_function(func)}"""
215aaaa20b6SVladimir Sementsov-Ogievskiy
216aaaa20b6SVladimir Sementsov-Ogievskiy
217aaaa20b6SVladimir Sementsov-Ogievskiydef gen_wrappers(input_code: str) -> str:
218aaaa20b6SVladimir Sementsov-Ogievskiy    res = ''
219aaaa20b6SVladimir Sementsov-Ogievskiy    for func in func_decl_iter(input_code):
220aaaa20b6SVladimir Sementsov-Ogievskiy        res += '\n\n\n'
221aaaa20b6SVladimir Sementsov-Ogievskiy        res += gen_wrapper(func)
222aaaa20b6SVladimir Sementsov-Ogievskiy
223aaaa20b6SVladimir Sementsov-Ogievskiy    return res
224aaaa20b6SVladimir Sementsov-Ogievskiy
225aaaa20b6SVladimir Sementsov-Ogievskiy
226aaaa20b6SVladimir Sementsov-Ogievskiyif __name__ == '__main__':
227aaaa20b6SVladimir Sementsov-Ogievskiy    if len(sys.argv) < 3:
228aaaa20b6SVladimir Sementsov-Ogievskiy        exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...')
229aaaa20b6SVladimir Sementsov-Ogievskiy
230aaaa20b6SVladimir Sementsov-Ogievskiy    with open(sys.argv[1], 'w', encoding='utf-8') as f_out:
231aaaa20b6SVladimir Sementsov-Ogievskiy        f_out.write(gen_header())
232aaaa20b6SVladimir Sementsov-Ogievskiy        for fname in sys.argv[2:]:
233aaaa20b6SVladimir Sementsov-Ogievskiy            with open(fname, encoding='utf-8') as f_in:
234aaaa20b6SVladimir Sementsov-Ogievskiy                f_out.write(gen_wrappers(f_in.read()))
235aaaa20b6SVladimir Sementsov-Ogievskiy                f_out.write('\n')
236