1/*
2  Usage:
3
4    spatch \
5        --macro-file scripts/cocci-macro-file.h \
6        --sp-file scripts/coccinelle/memory-region-housekeeping.cocci \
7        --keep-comments \
8        --in-place \
9        --dir .
10
11*/
12
13
14// Replace memory_region_init_ram(readonly) by memory_region_init_rom()
15@@
16expression E1, E2, E3, E4, E5;
17symbol true;
18@@
19(
20- memory_region_init_ram(E1, E2, E3, E4, E5);
21+ memory_region_init_rom(E1, E2, E3, E4, E5);
22  ... WHEN != E1
23- memory_region_set_readonly(E1, true);
24|
25- memory_region_init_ram_nomigrate(E1, E2, E3, E4, E5);
26+ memory_region_init_rom_nomigrate(E1, E2, E3, E4, E5);
27  ... WHEN != E1
28- memory_region_set_readonly(E1, true);
29)
30
31
32@possible_memory_region_init_rom@
33expression E1, E2, E3, E4, E5;
34position p;
35@@
36(
37  memory_region_init_ram@p(E1, E2, E3, E4, E5);
38  ...
39  memory_region_set_readonly(E1, true);
40|
41  memory_region_init_ram_nomigrate@p(E1, E2, E3, E4, E5);
42  ...
43  memory_region_set_readonly(E1, true);
44)
45@script:python@
46p << possible_memory_region_init_rom.p;
47@@
48cocci.print_main("potential use of memory_region_init_rom*() in ", p)
49
50
51// Do not call memory_region_set_readonly() on ROM alias
52@@
53expression ROM, E1, E2, E3, E4;
54expression ALIAS, E5, E6, E7, E8;
55@@
56(
57  memory_region_init_rom(ROM, E1, E2, E3, E4);
58|
59  memory_region_init_rom_nomigrate(ROM, E1, E2, E3, E4);
60)
61  ...
62   memory_region_init_alias(ALIAS, E5, E6, ROM, E7, E8);
63-  memory_region_set_readonly(ALIAS, true);
64
65
66// Replace by-hand memory_region_init_ram_nomigrate/vmstate_register_ram
67// code sequences with use of the new memory_region_init_ram function.
68// Similarly for the _rom and _rom_device functions.
69// We don't try to replace sequences with a non-NULL owner, because
70// there are none in the tree that can be automatically converted
71// (and only a handful that can be manually converted).
72@@
73expression MR;
74expression NAME;
75expression SIZE;
76expression ERRP;
77@@
78-memory_region_init_ram_nomigrate(MR, NULL, NAME, SIZE, ERRP);
79+memory_region_init_ram(MR, NULL, NAME, SIZE, ERRP);
80 ...
81-vmstate_register_ram_global(MR);
82@@
83expression MR;
84expression NAME;
85expression SIZE;
86expression ERRP;
87@@
88-memory_region_init_rom_nomigrate(MR, NULL, NAME, SIZE, ERRP);
89+memory_region_init_rom(MR, NULL, NAME, SIZE, ERRP);
90 ...
91-vmstate_register_ram_global(MR);
92@@
93expression MR;
94expression OPS;
95expression OPAQUE;
96expression NAME;
97expression SIZE;
98expression ERRP;
99@@
100-memory_region_init_rom_device_nomigrate(MR, NULL, OPS, OPAQUE, NAME, SIZE, ERRP);
101+memory_region_init_rom_device(MR, NULL, OPS, OPAQUE, NAME, SIZE, ERRP);
102 ...
103-vmstate_register_ram_global(MR);
104
105
106// Device is owner
107@@
108typedef DeviceState;
109identifier device_fn, dev, obj;
110expression E1, E2, E3, E4, E5;
111@@
112static void device_fn(DeviceState *dev, ...)
113{
114  ...
115  Object *obj = OBJECT(dev);
116  <+...
117(
118- memory_region_init(E1, NULL, E2, E3);
119+ memory_region_init(E1, obj, E2, E3);
120|
121- memory_region_init_io(E1, NULL, E2, E3, E4, E5);
122+ memory_region_init_io(E1, obj, E2, E3, E4, E5);
123|
124- memory_region_init_alias(E1, NULL, E2, E3, E4, E5);
125+ memory_region_init_alias(E1, obj, E2, E3, E4, E5);
126|
127- memory_region_init_rom(E1, NULL, E2, E3, E4);
128+ memory_region_init_rom(E1, obj, E2, E3, E4);
129|
130- memory_region_init_ram_flags_nomigrate(E1, NULL, E2, E3, E4, E5);
131+ memory_region_init_ram_flags_nomigrate(E1, obj, E2, E3, E4, E5);
132)
133  ...+>
134}
135@@
136identifier device_fn, dev;
137expression E1, E2, E3, E4, E5;
138@@
139static void device_fn(DeviceState *dev, ...)
140{
141  <+...
142(
143- memory_region_init(E1, NULL, E2, E3);
144+ memory_region_init(E1, OBJECT(dev), E2, E3);
145|
146- memory_region_init_io(E1, NULL, E2, E3, E4, E5);
147+ memory_region_init_io(E1, OBJECT(dev), E2, E3, E4, E5);
148|
149- memory_region_init_alias(E1, NULL, E2, E3, E4, E5);
150+ memory_region_init_alias(E1, OBJECT(dev), E2, E3, E4, E5);
151|
152- memory_region_init_rom(E1, NULL, E2, E3, E4);
153+ memory_region_init_rom(E1, OBJECT(dev), E2, E3, E4);
154|
155- memory_region_init_ram_flags_nomigrate(E1, NULL, E2, E3, E4, E5);
156+ memory_region_init_ram_flags_nomigrate(E1, OBJECT(dev), E2, E3, E4, E5);
157)
158  ...+>
159}
160