1
----------------------------------------------------------------------
2
Patch name: patch.example-user-plugin
8
This sample code can be used as a framework for writing user plugins.
9
It contains enough code for testing the existing user plugin support
10
in Bochs and creates a device that installs a 32-bit r/w register at
12
This example also shows how to install and handle user-defined config
13
options for bochsrc, command line and the config interface. In this
14
demo it is used to specify the reset value.
15
For user plugin support Bochs must be configured with plugins enabled.
16
Compiling with plugin support is known to work on Linux and Windows
17
(Cygwin or MinGW/MSYS). Bochs supports loading of up to 16 different
18
user plugins when starting up. Loading / removing plugins at runtime
21
Patch was created with:
23
Apply patch to what version:
24
cvs checked out on 09 Feb 2009
26
To patch, go to main bochs directory.
27
Type "patch -p0 < THIS_PATCH_FILE".
28
Regenerate the configure script and compile Bochs as usual
32
----------------------------------------------------------------------
33
diff -urN ../bochs/configure.in ./configure.in
34
--- ../bochs/configure.in 2009-02-02 14:02:47.000000000 +0100
35
+++ ./configure.in 2009-02-09 10:49:14.000000000 +0100
37
build/linux/bochs-dlx \
38
bxversion.h bxversion.rc build/macosx/Info.plist \
39
build/win32/nsis/Makefile build/win32/nsis/bochs.nsi \
40
- host/linux/pcidev/Makefile)
41
+ host/linux/pcidev/Makefile user-plugin/Makefile)
42
diff -urN ../bochs/user-plugin/Makefile.in ./user-plugin/Makefile.in
43
--- ../bochs/user-plugin/Makefile.in 1970-01-01 01:00:00.000000000 +0100
44
+++ ./user-plugin/Makefile.in 2009-02-09 10:49:14.000000000 +0100
46
+# Copyright (C) 2009 Volker Ruppert
48
+# This library is free software; you can redistribute it and/or
49
+# modify it under the terms of the GNU Lesser General Public
50
+# License as published by the Free Software Foundation; either
51
+# version 2 of the License, or (at your option) any later version.
53
+# This library is distributed in the hope that it will be useful,
54
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
55
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
56
+# Lesser General Public License for more details.
58
+# You should have received a copy of the GNU Lesser General Public
59
+# License along with this library; if not, write to the Free Software
60
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
62
+# Makefile for the user plugin example of bochs
68
+exec_prefix = @exec_prefix@
73
+plugdir = @libdir@/bochs/plugins
74
+datarootdir = @datarootdir@
76
+man1dir = $(mandir)/man1
77
+man5dir = $(mandir)/man5
78
+docdir = $(datarootdir)/doc/bochs
79
+sharedir = $(datarootdir)/bochs
81
+top_srcdir = @top_srcdir@
88
+CXXFLAGS = $(BX_INCDIRS) @CXXFLAGS@ @GUI_CXXFLAGS@
96
+WIN32_DLL_IMPORT_LIBRARY=../dllexports.a
98
+BX_INCDIRS = -I.. -I$(srcdir)/.. -I../iodev -I$(srcdir)/../iodev -I../@INSTRUMENT_DIR@ -I$(srcdir)/../@INSTRUMENT_DIR@
100
+PLUGIN_OBJS = testdev.o
102
+plugins: $(PLUGIN_OBJS:@PLUGIN_LIBNAME_TRANSFORMATION@)
104
+install: @INSTALL_PLUGINS_VAR@
106
+install_libtool_plugins::
107
+ list=`echo *.la`; for i in $$list; do $(LIBTOOL) --mode=install install $$i $(DESTDIR)$(plugdir); done
108
+ $(LIBTOOL) --finish $(DESTDIR)$(plugdir)
110
+install_dll_plugins::
111
+ list=`echo *.dll`; for i in $$list; do cp $$i $(DESTDIR)$(plugdir); done
113
+# standard compile rule for C++ files
115
+ $(CXX) @DASH@c $(CXXFLAGS) $(LOCAL_CXXFLAGS) @CXXFP@$< @OFP@$@
117
+##### building plugins with libtool
118
+%.lo: %.@CPP_SUFFIX@
119
+ $(LIBTOOL) --mode=compile $(CXX) -c $(CXXFLAGS) $(LOCAL_CXXFLAGS) $< -o $@
122
+ $(LIBTOOL) --mode=link $(CXX) -module $< -o $@ -rpath $(PLUGIN_PATH)
124
+#### building DLLs for win32 (tested on cygwin only)
126
+ $(CXX) $(CXXFLAGS) -shared -o $@ $< $(WIN32_DLL_IMPORT_LIBRARY)
129
+ @RMCOMMAND@ -rf .libs *.lo *.o *.la *.a *.dll
132
+ @RMCOMMAND@ Makefile
134
+###########################################
135
+# dependencies generated by
136
+# gcc -MM -I.. -I../instrument/stubs *.cc | sed -e 's/\.cc/.@CPP_SUFFIX@/g'
137
+# gcc -MM -I.. -I../instrument/stubs *.cc | \
138
+# sed -e 's/\.cc/.@CPP_SUFFIX@/g' -e 's/\.o:/.lo:/g'
140
+# This means that every source file is listed twice, once with a .o rule
141
+# and then again with an identical .lo rule. The .lo rules are used when
143
+###########################################
144
+testdev.o: testdev.@CPP_SUFFIX@ ../iodev/iodev.h ../bochs.h ../config.h ../osdep.h \
145
+ ../bx_debug/debug.h ../config.h ../osdep.h ../bxversion.h \
146
+ ../gui/siminterface.h ../memory/memory.h ../pc_system.h ../plugin.h \
147
+ ../extplugin.h ../ltdl.h ../gui/gui.h ../gui/textconfig.h \
148
+ ../gui/keymap.h ../instrument/stubs/instrument.h testdev.h
149
+testdev.lo: testdev.@CPP_SUFFIX@ ../iodev/iodev.h ../bochs.h ../config.h ../osdep.h \
150
+ ../bx_debug/debug.h ../config.h ../osdep.h ../bxversion.h \
151
+ ../gui/siminterface.h ../memory/memory.h ../pc_system.h ../plugin.h \
152
+ ../extplugin.h ../ltdl.h ../gui/gui.h ../gui/textconfig.h \
153
+ ../gui/keymap.h ../instrument/stubs/instrument.h testdev.h
154
diff -urN ../bochs/user-plugin/testdev.cc ./user-plugin/testdev.cc
155
--- ../bochs/user-plugin/testdev.cc 1970-01-01 01:00:00.000000000 +0100
156
+++ ./user-plugin/testdev.cc 2009-02-09 10:49:14.000000000 +0100
158
+// Copyright (C) 2009 Volker Ruppert
160
+// This library is free software; you can redistribute it and/or
161
+// modify it under the terms of the GNU Lesser General Public
162
+// License as published by the Free Software Foundation; either
163
+// version 2 of the License, or (at your option) any later version.
165
+// This library is distributed in the hope that it will be useful,
166
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
167
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
168
+// Lesser General Public License for more details.
170
+// You should have received a copy of the GNU Lesser General Public
171
+// License along with this library; if not, write to the Free Software
172
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
174
+// User plugin example (see patch description for details)
176
+// Define BX_PLUGGABLE in files that can be compiled into plugins. For
177
+// platforms that require a special tag on exported symbols, BX_PLUGGABLE
178
+// is used to know when we are exporting symbols and when we are importing.
179
+#define BX_PLUGGABLE
181
+// Don't include other devices headers
182
+#define NO_DEVICE_INCLUDES
185
+#include "testdev.h"
187
+#define LOG_THIS theTestDevice->
189
+bx_testdev_c *theTestDevice = NULL;
191
+Bit32s testdev_options_parser(const char *context, int num_params, char *params[]);
192
+Bit32s testdev_options_save(FILE *fp);
194
+int libuser_LTX_plugin_init(plugin_t *plugin, plugintype_t type, int argc, char *argv[])
196
+ theTestDevice = new bx_testdev_c();
197
+ BX_REGISTER_DEVICE_DEVMODEL(plugin, type, theTestDevice, "testdev");
198
+ // add new configuration parameter for the config interface
199
+ bx_param_c *root_param = SIM->get_param("user");
200
+ bx_list_c *menu = new bx_list_c(root_param, "testdev", "Test Device");
201
+ menu->get_options()->set(bx_list_c::SHOW_PARENT);
202
+ new bx_param_num_c(menu, "test", "Test Parameter", "", 0, BX_MAX_BIT32U, 0);
203
+ // register user-defined option for bochsrc and command line
204
+ SIM->register_user_option("testdev", testdev_options_parser, testdev_options_save);
205
+ return(0); // Success
208
+void libuser_LTX_plugin_fini(void)
210
+ SIM->unregister_user_option("testdev");
211
+ bx_list_c *menu = (bx_list_c*)SIM->get_param("user");
212
+ menu->remove("testdev");
213
+ delete theTestDevice;
216
+Bit32s testdev_options_parser(const char *context, int num_params, char *params[])
218
+ if (!strcmp(params[0], "testdev")) {
219
+ if (!strncmp(params[1], "test=", 5)) {
220
+ SIM->get_param_num("user.testdev.test")->set(atoi(¶ms[1][5]));
222
+ BX_PANIC(("%s: testdev: unknown parameter '%s'", context, params[1]));
225
+ BX_PANIC(("%s: unknown directive '%s'", context, params[0]));
230
+Bit32s testdev_options_save(FILE *fp)
232
+ fprintf(fp, "testdev: test=%d\n", SIM->get_param_num("user.testdev.test")->get());
236
+bx_testdev_c::bx_testdev_c(void)
241
+bx_testdev_c::~bx_testdev_c(void)
243
+ // nothing here yet
246
+void bx_testdev_c::init(void)
248
+ DEV_register_ioread_handler(this, read_handler, 0x1000, "Test Device", 4);
249
+ DEV_register_iowrite_handler(this, write_handler, 0x1000, "Test Device", 4);
252
+void bx_testdev_c::reset(unsigned type)
254
+ BX_USER_THIS s.reg0 = SIM->get_param_num("user.testdev.test")->get();
257
+void bx_testdev_c::register_state(void)
259
+ bx_list_c *list = new bx_list_c(SIM->get_bochs_root(), "testdev", "Test Device", 1);
260
+ BXRS_HEX_PARAM_FIELD(list, reg0, BX_USER_THIS s.reg0);
263
+Bit32u bx_testdev_c::read_handler(void *this_ptr, Bit32u address, unsigned io_len)
267
+ return BX_USER_THIS s.reg0;
270
+void bx_testdev_c::write_handler(void *this_ptr, Bit32u address, Bit32u value, unsigned io_len)
274
+ BX_USER_THIS s.reg0 = value;
276
diff -urN ../bochs/user-plugin/testdev.h ./user-plugin/testdev.h
277
--- ../bochs/user-plugin/testdev.h 1970-01-01 01:00:00.000000000 +0100
278
+++ ./user-plugin/testdev.h 2009-02-09 10:49:14.000000000 +0100
280
+// Copyright (C) 2009 Volker Ruppert
282
+// This library is free software; you can redistribute it and/or
283
+// modify it under the terms of the GNU Lesser General Public
284
+// License as published by the Free Software Foundation; either
285
+// version 2 of the License, or (at your option) any later version.
287
+// This library is distributed in the hope that it will be useful,
288
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
289
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
290
+// Lesser General Public License for more details.
292
+// You should have received a copy of the GNU Lesser General Public
293
+// License along with this library; if not, write to the Free Software
294
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
296
+#ifndef BX_TESTDEV_H
297
+#define BX_TESTDEV_H
299
+#define BX_USER_THIS theTestDevice->
301
+class bx_testdev_c : public bx_devmodel_c {
304
+ virtual ~bx_testdev_c();
306
+ virtual void init(void);
307
+ virtual void reset(unsigned type);
308
+ virtual void register_state(void);
311
+ static Bit32u read_handler(void *this_ptr, Bit32u address, unsigned io_len);
312
+ static void write_handler(void *this_ptr, Bit32u address, Bit32u value, unsigned io_len);