-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.cc
926 lines (798 loc) · 26.6 KB
/
plugin.cc
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
/////////////////////////////////////////////////////////////////////////
// $Id: plugin.cc 11419 2012-09-14 21:50:50Z vruppert $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2002-2011 The Bochs Project
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
/////////////////////////////////////////////////////////////////////////
//
// This file defines the plugin and plugin-device registration functions and
// the device registration functions. It handles dynamic loading of modules,
// using the LTDL library for cross-platform support.
//
// This file is based on the plugin.c file from plex86, but with significant
// changes to make it work in Bochs.
// Plex86 is Copyright (C) 1999-2000 The plex86 developers team
//
/////////////////////////////////////////////////////////////////////////
#include "bochs.h"
#include "iodev/iodev.h"
#include "plugin.h"
#define LOG_THIS genlog->
#define PLUGIN_INIT_FMT_STRING "lib%s_LTX_plugin_init"
#define PLUGIN_FINI_FMT_STRING "lib%s_LTX_plugin_fini"
#define PLUGIN_PATH ""
#ifndef WIN32
#define PLUGIN_FILENAME_FORMAT "libbx_%s.so"
#else
#define PLUGIN_FILENAME_FORMAT "bx_%s.dll"
#endif
extern "C" {
void (*pluginRegisterIRQ)(unsigned irq, const char* name) = 0;
void (*pluginUnregisterIRQ)(unsigned irq, const char* name) = 0;
void (*pluginSetHRQ)(unsigned val) = 0;
void (*pluginSetHRQHackCallback)(void (*callback)(void)) = 0;
int (*pluginRegisterIOReadHandler)(void *thisPtr, ioReadHandler_t callback,
unsigned base, const char *name, Bit8u mask) = 0;
int (*pluginRegisterIOWriteHandler)(void *thisPtr, ioWriteHandler_t callback,
unsigned base, const char *name, Bit8u mask) = 0;
int (*pluginUnregisterIOReadHandler)(void *thisPtr, ioReadHandler_t callback,
unsigned base, Bit8u mask) = 0;
int (*pluginUnregisterIOWriteHandler)(void *thisPtr, ioWriteHandler_t callback,
unsigned base, Bit8u mask) = 0;
int (*pluginRegisterIOReadHandlerRange)(void *thisPtr, ioReadHandler_t callback,
unsigned base, unsigned end, const char *name, Bit8u mask) = 0;
int (*pluginRegisterIOWriteHandlerRange)(void *thisPtr, ioWriteHandler_t callback,
unsigned base, unsigned end, const char *name, Bit8u mask) = 0;
int (*pluginUnregisterIOReadHandlerRange)(void *thisPtr, ioReadHandler_t callback,
unsigned begin, unsigned end, Bit8u mask) = 0;
int (*pluginUnregisterIOWriteHandlerRange)(void *thisPtr, ioWriteHandler_t callback,
unsigned begin, unsigned end, Bit8u mask) = 0;
int (*pluginRegisterDefaultIOReadHandler)(void *thisPtr, ioReadHandler_t callback,
const char *name, Bit8u mask) = 0;
int (*pluginRegisterDefaultIOWriteHandler)(void *thisPtr, ioWriteHandler_t callback,
const char *name, Bit8u mask) = 0;
int (*pluginRegisterTimer)(void *this_ptr, void (*funct)(void *),
Bit32u useconds, bx_bool continuous,
bx_bool active, const char* name) = 0;
void (*pluginActivateTimer)(unsigned id, Bit32u usec, bx_bool continuous) = 0;
void (*pluginHRQHackCallback)(void);
unsigned pluginHRQ = 0;
plugin_t *plugins = NULL; /* Head of the linked list of plugins */
#if BX_PLUGINS
static void plugin_init_one(plugin_t *plugin);
#endif
device_t *devices = NULL; /* Head of the linked list of registered devices */
device_t *core_devices = NULL; /* Head of the linked list of registered core devices */
plugin_t *current_plugin_context = NULL;
/************************************************************************/
/* Builtins declarations */
/************************************************************************/
static void
builtinRegisterIRQ(unsigned irq, const char* name)
{
#if 0
pluginlog->panic("builtinRegisterIRQ called, no pic plugin loaded?");
#else
bx_devices.register_irq(irq, name);
#endif
}
static void
builtinUnregisterIRQ(unsigned irq, const char* name)
{
#if 0
pluginlog->panic("builtinUnregisterIRQ called, no pic plugin loaded?");
#else
bx_devices.unregister_irq(irq, name);
#endif
}
static void
builtinSetHRQ(unsigned val)
{
#if 0
pluginlog->panic("builtinSetHRQ called, no plugin loaded?");
#else
pluginHRQ = val;
#endif
}
static void
builtinSetHRQHackCallback(void (*callback)(void))
{
#if 0
pluginlog->panic("builtinSetHRQHackCallback called, no plugin loaded?");
#else
pluginHRQHackCallback = callback;
#endif
}
static int
builtinRegisterIOReadHandler(void *thisPtr, ioReadHandler_t callback,
unsigned base, const char *name, Bit8u mask)
{
int ret;
BX_ASSERT(mask<8);
ret = bx_devices.register_io_read_handler (thisPtr, callback, base, name, mask);
pluginlog->ldebug("plugin %s registered I/O read address at %04x", name, base);
return ret;
}
static int
builtinRegisterIOWriteHandler(void *thisPtr, ioWriteHandler_t callback,
unsigned base, const char *name, Bit8u mask)
{
int ret;
BX_ASSERT(mask<8);
ret = bx_devices.register_io_write_handler (thisPtr, callback, base, name, mask);
pluginlog->ldebug("plugin %s registered I/O write address at %04x", name, base);
return ret;
}
static int
builtinUnregisterIOReadHandler(void *thisPtr, ioReadHandler_t callback,
unsigned base, Bit8u mask)
{
int ret;
BX_ASSERT(mask<8);
ret = bx_devices.unregister_io_read_handler (thisPtr, callback, base, mask);
pluginlog->ldebug("plugin unregistered I/O read address at %04x", base);
return ret;
}
static int
builtinUnregisterIOWriteHandler(void *thisPtr, ioWriteHandler_t callback,
unsigned base, Bit8u mask)
{
int ret;
BX_ASSERT(mask<8);
ret = bx_devices.unregister_io_write_handler (thisPtr, callback, base, mask);
pluginlog->ldebug("plugin unregistered I/O write address at %04x", base);
return ret;
}
static int
builtinRegisterIOReadHandlerRange(void *thisPtr, ioReadHandler_t callback,
unsigned base, unsigned end, const char *name, Bit8u mask)
{
int ret;
BX_ASSERT(mask<8);
ret = bx_devices.register_io_read_handler_range (thisPtr, callback, base, end, name, mask);
pluginlog->ldebug("plugin %s registered I/O read addresses %04x to %04x", name, base, end);
return ret;
}
static int
builtinRegisterIOWriteHandlerRange(void *thisPtr, ioWriteHandler_t callback,
unsigned base, unsigned end, const char *name, Bit8u mask)
{
int ret;
BX_ASSERT(mask<8);
ret = bx_devices.register_io_write_handler_range (thisPtr, callback, base, end, name, mask);
pluginlog->ldebug("plugin %s registered I/O write addresses %04x to %04x", name, base, end);
return ret;
}
static int
builtinUnregisterIOReadHandlerRange(void *thisPtr, ioReadHandler_t callback,
unsigned begin, unsigned end, Bit8u mask)
{
int ret;
BX_ASSERT(mask<8);
ret = bx_devices.unregister_io_read_handler_range (thisPtr, callback, begin, end, mask);
pluginlog->ldebug("plugin unregistered I/O read addresses %04x to %04x", begin, end);
return ret;
}
static int
builtinUnregisterIOWriteHandlerRange(void *thisPtr, ioWriteHandler_t callback,
unsigned begin, unsigned end, Bit8u mask)
{
int ret;
BX_ASSERT(mask<8);
ret = bx_devices.unregister_io_write_handler_range (thisPtr, callback, begin, end, mask);
pluginlog->ldebug("plugin unregistered I/O write addresses %04x to %04x", begin, end);
return ret;
}
static int
builtinRegisterDefaultIOReadHandler(void *thisPtr, ioReadHandler_t callback,
const char *name, Bit8u mask)
{
BX_ASSERT(mask<8);
bx_devices.register_default_io_read_handler (thisPtr, callback, name, mask);
pluginlog->ldebug("plugin %s registered default I/O read ", name);
return 0;
}
static int
builtinRegisterDefaultIOWriteHandler(void *thisPtr, ioWriteHandler_t callback,
const char *name, Bit8u mask)
{
BX_ASSERT(mask<8);
bx_devices.register_default_io_write_handler (thisPtr, callback, name, mask);
pluginlog->ldebug("plugin %s registered default I/O write ", name);
return 0;
}
static int
builtinRegisterTimer(void *this_ptr, void (*funct)(void *),
Bit32u useconds, bx_bool continuous,
bx_bool active, const char* name)
{
int id = bx_pc_system.register_timer (this_ptr, funct, useconds, continuous, active, name);
pluginlog->ldebug("plugin %s registered timer %d", name, id);
return id;
}
static void
builtinActivateTimer(unsigned id, Bit32u usec, bx_bool continuous)
{
bx_pc_system.activate_timer (id, usec, continuous);
pluginlog->ldebug("plugin activated timer %d", id);
}
#if BX_PLUGINS
/************************************************************************/
/* Plugin initialization / deinitialization */
/************************************************************************/
void plugin_init_all (void)
{
plugin_t *plugin;
pluginlog->info("Initializing plugins");
for (plugin = plugins; plugin; plugin = plugin->next)
{
char *arg_ptr = plugin->args;
/* process the command line */
plugin->argc = 0;
while (plugin->argc < MAX_ARGC)
{
while (*arg_ptr && isspace (*arg_ptr))
arg_ptr++;
if (!*arg_ptr) break;
plugin->argv[plugin->argc++] = arg_ptr;
while (*arg_ptr && !isspace (*arg_ptr))
arg_ptr++;
if (!*arg_ptr) break;
*arg_ptr++ = '\0';
}
/* initialize the plugin */
if (plugin->plugin_init (plugin, plugin->type, plugin->argc, plugin->argv))
{
pluginlog->panic("Plugin initialization failed for %s", plugin->name);
plugin_abort();
}
plugin->initialized = 1;
}
}
void plugin_init_one(plugin_t *plugin)
{
char *arg_ptr = plugin->args;
/* process the command line */
plugin->argc = 0;
while (plugin->argc < MAX_ARGC)
{
while (*arg_ptr && isspace (*arg_ptr))
arg_ptr++;
if (!*arg_ptr) break;
plugin->argv[plugin->argc++] = arg_ptr;
while (*arg_ptr && !isspace (*arg_ptr))
arg_ptr++;
if (!*arg_ptr) break;
*arg_ptr++ = '\0';
}
/* initialize the plugin */
if (plugin->plugin_init (plugin, plugin->type, plugin->argc, plugin->argv))
{
pluginlog->info("Plugin initialization failed for %s", plugin->name);
plugin_abort();
}
plugin->initialized = 1;
}
plugin_t *plugin_unload(plugin_t *plugin)
{
plugin_t *dead_plug;
if (plugin->initialized)
plugin->plugin_fini();
#if defined(_MSC_VER)
FreeLibrary(plugin->handle);
#else
lt_dlclose(plugin->handle);
#endif
delete [] plugin->name;
dead_plug = plugin;
plugin = plugin->next;
free(dead_plug);
return plugin;
}
void plugin_fini_all (void)
{
plugin_t *plugin;
for (plugin = plugins; plugin; plugin = plugin_unload(plugin));
}
void plugin_load(char *name, char *args, plugintype_t type)
{
plugin_t *plugin, *temp;
if (plugins != NULL) {
temp = plugins;
while (temp != NULL) {
if (!strcmp(name, temp->name)) {
BX_PANIC(("plugin '%s' already loaded", name));
return;
}
temp = temp->next;
}
}
plugin = (plugin_t *)malloc (sizeof(plugin_t));
if (!plugin)
{
BX_PANIC(("malloc plugin_t failed"));
}
plugin->type = type;
plugin->name = name;
plugin->args = args;
plugin->initialized = 0;
char plugin_filename[BX_PATHNAME_LEN], buf[BX_PATHNAME_LEN];
sprintf(buf, PLUGIN_FILENAME_FORMAT, name);
sprintf(plugin_filename, "%s%s", PLUGIN_PATH, buf);
// Set context so that any devices that the plugin registers will
// be able to see which plugin created them. The registration will
// be called from either dlopen (global constructors) or plugin_init.
BX_ASSERT(current_plugin_context == NULL);
current_plugin_context = plugin;
#if defined(_MSC_VER)
plugin->handle = LoadLibrary(plugin_filename);
BX_INFO(("DLL handle is %p", plugin->handle));
if (!plugin->handle)
{
current_plugin_context = NULL;
BX_PANIC(("LoadLibrary failed for module '%s': error=%d", name, GetLastError()));
free(plugin);
return;
}
#else
plugin->handle = lt_dlopen (plugin_filename);
BX_INFO(("lt_dlhandle is %p", plugin->handle));
if (!plugin->handle)
{
current_plugin_context = NULL;
BX_PANIC(("dlopen failed for module '%s': %s", name, lt_dlerror ()));
free(plugin);
return;
}
#endif
if (type != PLUGTYPE_USER) {
sprintf(buf, PLUGIN_INIT_FMT_STRING, name);
} else {
sprintf(buf, PLUGIN_INIT_FMT_STRING, "user");
}
#if defined(_MSC_VER)
plugin->plugin_init = (plugin_init_t) GetProcAddress(plugin->handle, buf);
if (plugin->plugin_init == NULL) {
pluginlog->panic("could not find plugin_init: error=%d", GetLastError());
plugin_abort ();
}
#else
plugin->plugin_init = (plugin_init_t) lt_dlsym (plugin->handle, buf);
if (plugin->plugin_init == NULL) {
pluginlog->panic("could not find plugin_init: %s", lt_dlerror ());
plugin_abort ();
}
#endif
if (type != PLUGTYPE_USER) {
sprintf(buf, PLUGIN_FINI_FMT_STRING, name);
} else {
sprintf(buf, PLUGIN_FINI_FMT_STRING, "user");
}
#if defined(_MSC_VER)
plugin->plugin_fini = (plugin_fini_t) GetProcAddress(plugin->handle, buf);
if (plugin->plugin_fini == NULL) {
pluginlog->panic("could not find plugin_fini: error=%d", GetLastError());
plugin_abort ();
}
#else
plugin->plugin_fini = (plugin_fini_t) lt_dlsym (plugin->handle, buf);
if (plugin->plugin_fini == NULL) {
pluginlog->panic("could not find plugin_fini: %s", lt_dlerror ());
plugin_abort();
}
#endif
pluginlog->info("loaded plugin %s",plugin_filename);
/* Insert plugin at the _end_ of the plugin linked list. */
plugin->next = NULL;
if (!plugins)
{
/* Empty list, this become the first entry. */
plugins = plugin;
}
else
{
/* Non-empty list. Add to end. */
temp = plugins;
while (temp->next)
temp = temp->next;
temp->next = plugin;
}
plugin_init_one(plugin);
// check that context didn't change. This should only happen if we
// need a reentrant plugin_load.
BX_ASSERT(current_plugin_context == plugin);
current_plugin_context = NULL;
}
void plugin_abort(void)
{
pluginlog->panic("plugin load aborted");
}
#endif /* end of #if BX_PLUGINS */
/************************************************************************/
/* Plugin system: initialisation of plugins entry points */
/************************************************************************/
void
plugin_startup(void)
{
pluginRegisterIRQ = builtinRegisterIRQ;
pluginUnregisterIRQ = builtinUnregisterIRQ;
pluginSetHRQHackCallback = builtinSetHRQHackCallback;
pluginSetHRQ = builtinSetHRQ;
pluginRegisterIOReadHandler = builtinRegisterIOReadHandler;
pluginRegisterIOWriteHandler = builtinRegisterIOWriteHandler;
pluginUnregisterIOReadHandler = builtinUnregisterIOReadHandler;
pluginUnregisterIOWriteHandler = builtinUnregisterIOWriteHandler;
pluginRegisterIOReadHandlerRange = builtinRegisterIOReadHandlerRange;
pluginRegisterIOWriteHandlerRange = builtinRegisterIOWriteHandlerRange;
pluginUnregisterIOReadHandlerRange = builtinUnregisterIOReadHandlerRange;
pluginUnregisterIOWriteHandlerRange = builtinUnregisterIOWriteHandlerRange;
pluginRegisterDefaultIOReadHandler = builtinRegisterDefaultIOReadHandler;
pluginRegisterDefaultIOWriteHandler = builtinRegisterDefaultIOWriteHandler;
pluginRegisterTimer = builtinRegisterTimer;
pluginActivateTimer = builtinActivateTimer;
#if BX_PLUGINS
pluginlog = new logfunctions();
pluginlog->put("PLGIN");
#if !defined(_MSC_VER)
int status = lt_dlinit ();
if (status != 0) {
BX_ERROR (("initialization error in ltdl library (for loading plugins)"));
BX_PANIC (("error message was: %s", lt_dlerror ()));
}
#endif
#endif
}
/************************************************************************/
/* Plugin system: Device registration */
/************************************************************************/
void pluginRegisterDeviceDevmodel(plugin_t *plugin, plugintype_t type, bx_devmodel_c *devmodel, const char *name)
{
device_t *device, **devlist;
device = (device_t *)malloc (sizeof (device_t));
if (!device)
{
pluginlog->panic("can't allocate device_t");
}
device->name = name;
BX_ASSERT(devmodel != NULL);
device->devmodel = devmodel;
device->plugin = plugin; // this can be NULL
device->next = NULL;
device->plugtype = type;
switch (type) {
case PLUGTYPE_CORE:
devlist = &core_devices;
break;
case PLUGTYPE_STANDARD:
case PLUGTYPE_OPTIONAL:
case PLUGTYPE_USER:
default:
devlist = &devices;
break;
}
if (!*devlist) {
/* Empty list, this become the first entry. */
*devlist = device;
} else {
/* Non-empty list. Add to end. */
device_t *temp = *devlist;
while (temp->next)
temp = temp->next;
temp->next = device;
}
}
/************************************************************************/
/* Plugin system: Remove registered plugin device */
/************************************************************************/
void pluginUnregisterDeviceDevmodel(const char *name)
{
device_t *device, *prev = NULL;
for (device = devices; device; device = device->next) {
if (!strcmp(name, device->name)) {
if (prev == NULL) {
devices = device->next;
} else {
prev->next = device->next;
}
free(device);
break;
} else {
prev = device;
}
}
}
/************************************************************************/
/* Plugin system: Check if a plugin is loaded */
/************************************************************************/
bx_bool pluginDevicePresent(const char *name)
{
device_t *device;
for (device = devices; device; device = device->next)
{
if (!strcmp(name, device->name)) return 1;
}
return 0;
}
#if BX_PLUGINS
/************************************************************************/
/* Plugin system: Load one plugin */
/************************************************************************/
int bx_load_plugin(const char *name, plugintype_t type)
{
char *namecopy = new char[1+strlen(name)];
strcpy(namecopy, name);
plugin_load(namecopy, (char*)"", type);
return 1;
}
void bx_unload_plugin(const char *name, bx_bool devflag)
{
plugin_t *plugin, *prev = NULL;
for (plugin = plugins; plugin; plugin = plugin->next) {
if (!strcmp(plugin->name, name)) {
if (devflag) {
pluginUnregisterDeviceDevmodel(plugin->name);
}
plugin = plugin_unload(plugin);
if (prev == NULL) {
plugins = plugin;
} else {
prev->next = plugin;
}
break;
} else {
prev = plugin;
}
}
}
#endif /* end of #if BX_PLUGINS */
/*************************************************************************/
/* Plugin system: Execute init function of all registered plugin-devices */
/*************************************************************************/
void bx_init_plugins()
{
device_t *device;
for (device = core_devices; device; device = device->next) {
pluginlog->info("init_dev of '%s' plugin device by virtual method",device->name);
device->devmodel->init();
}
for (device = devices; device; device = device->next) {
if (device->plugtype == PLUGTYPE_STANDARD) {
pluginlog->info("init_dev of '%s' plugin device by virtual method",device->name);
device->devmodel->init();
}
}
for (device = devices; device; device = device->next) {
if (device->plugtype == PLUGTYPE_OPTIONAL) {
pluginlog->info("init_dev of '%s' plugin device by virtual method",device->name);
device->devmodel->init();
}
}
#if BX_PLUGINS
for (device = devices; device; device = device->next) {
if (device->plugtype == PLUGTYPE_USER) {
pluginlog->info("init_dev of '%s' plugin device by virtual method",device->name);
device->devmodel->init();
}
}
#endif
}
/**************************************************************************/
/* Plugin system: Execute reset function of all registered plugin-devices */
/**************************************************************************/
void bx_reset_plugins(unsigned signal)
{
device_t *device;
for (device = core_devices; device; device = device->next) {
pluginlog->info("reset of '%s' plugin device by virtual method",device->name);
device->devmodel->reset(signal);
}
for (device = devices; device; device = device->next) {
if (device->plugtype == PLUGTYPE_STANDARD) {
pluginlog->info("reset of '%s' plugin device by virtual method",device->name);
device->devmodel->reset(signal);
}
}
for (device = devices; device; device = device->next) {
if (device->plugtype == PLUGTYPE_OPTIONAL) {
pluginlog->info("reset of '%s' plugin device by virtual method",device->name);
device->devmodel->reset(signal);
}
}
#if BX_PLUGINS
for (device = devices; device; device = device->next) {
if (device->plugtype == PLUGTYPE_USER) {
pluginlog->info("reset of '%s' plugin device by virtual method",device->name);
device->devmodel->reset(signal);
}
}
#endif
}
/*******************************************************/
/* Plugin system: Unload all registered plugin-devices */
/*******************************************************/
void bx_unload_plugins()
{
device_t *device, *next;
device = devices;
while (device != NULL) {
if (device->plugin != NULL) {
#if BX_PLUGINS
bx_unload_plugin(device->name, 0);
#endif
} else {
#if !BX_PLUGINS
if (!bx_unload_opt_plugin(device->name, 0)) {
delete device->devmodel;
}
#endif
}
next = device->next;
free(device);
device = next;
}
devices = NULL;
}
void bx_unload_core_plugins()
{
device_t *device, *next;
device = core_devices;
while (device != NULL) {
if (device->plugin != NULL) {
#if BX_PLUGINS
bx_unload_plugin(device->name, 0);
#endif
} else {
delete device->devmodel;
}
next = device->next;
free(device);
device = next;
}
core_devices = NULL;
}
/**************************************************************************/
/* Plugin system: Register device state of all registered plugin-devices */
/**************************************************************************/
void bx_plugins_register_state()
{
device_t *device;
for (device = core_devices; device; device = device->next) {
pluginlog->info("register state of '%s' plugin device by virtual method",device->name);
device->devmodel->register_state();
}
for (device = devices; device; device = device->next) {
pluginlog->info("register state of '%s' plugin device by virtual method",device->name);
device->devmodel->register_state();
}
}
/***************************************************************************/
/* Plugin system: Execute code after restoring state of all plugin devices */
/***************************************************************************/
void bx_plugins_after_restore_state()
{
device_t *device;
for (device = core_devices; device; device = device->next) {
device->devmodel->after_restore_state();
}
for (device = devices; device; device = device->next) {
if (device->plugtype == PLUGTYPE_STANDARD) {
device->devmodel->after_restore_state();
}
}
for (device = devices; device; device = device->next) {
if (device->plugtype == PLUGTYPE_OPTIONAL) {
device->devmodel->after_restore_state();
}
}
#if BX_PLUGINS
for (device = devices; device; device = device->next) {
if (device->plugtype == PLUGTYPE_USER) {
device->devmodel->after_restore_state();
}
}
#endif
}
#if !BX_PLUGINS
// special code for loading optional plugins when plugins are turned off
typedef struct {
const char *name;
plugin_init_t plugin_init;
plugin_fini_t plugin_fini;
bx_bool status;
} builtin_plugin_t;
#define BUILTIN_PLUGIN_ENTRY(mod) {#mod, lib##mod##_LTX_plugin_init, lib##mod##_LTX_plugin_fini, 0}
static builtin_plugin_t builtin_opt_plugins[] = {
BUILTIN_PLUGIN_ENTRY(unmapped),
BUILTIN_PLUGIN_ENTRY(biosdev),
BUILTIN_PLUGIN_ENTRY(speaker),
BUILTIN_PLUGIN_ENTRY(extfpuirq),
BUILTIN_PLUGIN_ENTRY(parallel),
BUILTIN_PLUGIN_ENTRY(serial),
#if BX_SUPPORT_E1000
BUILTIN_PLUGIN_ENTRY(e1000),
#endif
#if BX_SUPPORT_ES1370
BUILTIN_PLUGIN_ENTRY(es1370),
#endif
#if BX_SUPPORT_GAMEPORT
BUILTIN_PLUGIN_ENTRY(gameport),
#endif
#if BX_SUPPORT_IODEBUG
BUILTIN_PLUGIN_ENTRY(iodebug),
#endif
#if BX_SUPPORT_NE2K
BUILTIN_PLUGIN_ENTRY(ne2k),
#endif
#if BX_SUPPORT_PCIDEV
BUILTIN_PLUGIN_ENTRY(pcidev),
#endif
#if BX_SUPPORT_PCIPNIC
BUILTIN_PLUGIN_ENTRY(pcipnic),
#endif
#if BX_SUPPORT_SB16
BUILTIN_PLUGIN_ENTRY(sb16),
#endif
#if BX_SUPPORT_USB_OHCI
BUILTIN_PLUGIN_ENTRY(usb_ohci),
#endif
#if BX_SUPPORT_USB_UHCI
BUILTIN_PLUGIN_ENTRY(usb_uhci),
#endif
#if BX_SUPPORT_USB_XHCI
BUILTIN_PLUGIN_ENTRY(usb_xhci),
#endif
#if BX_SUPPORT_VOODOO
BUILTIN_PLUGIN_ENTRY(voodoo),
#endif
{"NULL", NULL, NULL, 0}
};
int bx_load_opt_plugin(const char *name)
{
int i = 0;
while (strcmp(builtin_opt_plugins[i].name, "NULL")) {
if (!strcmp(name, builtin_opt_plugins[i].name)) {
if (builtin_opt_plugins[i].status == 0) {
builtin_opt_plugins[i].plugin_init(NULL, PLUGTYPE_OPTIONAL, 0, NULL);
builtin_opt_plugins[i].status = 1;
}
return 1;
}
i++;
};
return 0;
}
int bx_unload_opt_plugin(const char *name, bx_bool devflag)
{
int i = 0;
while (strcmp(builtin_opt_plugins[i].name, "NULL")) {
if (!strcmp(name, builtin_opt_plugins[i].name)) {
if (builtin_opt_plugins[i].status == 1) {
if (devflag) {
pluginUnregisterDeviceDevmodel(builtin_opt_plugins[i].name);
}
builtin_opt_plugins[i].plugin_fini();
builtin_opt_plugins[i].status = 0;
}
return 1;
}
i++;
};
return 0;
}
#endif
}