File: plugin.c

package info (click to toggle)
plex86 0.0.20011018-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,868 kB
  • ctags: 8,721
  • sloc: ansic: 46,915; cpp: 17,817; xml: 1,283; makefile: 1,130; sh: 451; asm: 360; csh: 18
file content (799 lines) | stat: -rw-r--r-- 18,182 bytes parent folder | download
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
/*
 *  plex86: run multiple x86 operating systems concurrently
 *  Copyright (C) 1999-2000  The plex86 developers team
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>

#include "plex86.h"
#include "user.h"
#include "plugin.h"



/************************************************************************/
/* Plugin initialization / deinitialization                             */
/************************************************************************/

static void plugin_init_one(plugin_t *plugin);

/* Some builtin (default pass-through) Write-Cache functions, when
 * no Write-Cache plugins are loaded to override these.  These are
 * strict pass-through functions
 */

static Bit64u   builtinWCLSeek(int fd, Bit64u off64, int whence);
static unsigned builtinWCRead(int fd, void *buffer, unsigned len);
static unsigned builtinWCWrite(int fd, void *buffer, unsigned len);
static void     builtinWCCommit(int fd);

static Bit32u   builtinHDReadHandler(Bit32u address,
    unsigned io_len);
static void     builtinHDWriteHandler(Bit32u address,
    Bit32u value, unsigned io_len);
static void     builtinVGARedrawArea(unsigned x0, unsigned y0,
                  unsigned width, unsigned height);


pluginCallbacks_t pluginCallbacks;

plugin_t *plugins = NULL;      /* Head of the linked list of plugins  */
void (*pluginHRQHackCallback)(void);
unsigned pluginHRQ = 0;

extern int filehdl; /* File handle for /dev/plex86 */

Bit32u   (* pluginGetCMOSReg)(unsigned reg) = 0;
void     (* pluginSetCMOSReg)(unsigned reg, Bit32u val) = 0;
void     (* pluginCMOSChecksum)(void) = 0;
void     (* pluginMouseMotion)(int d_x, int d_y, unsigned button_state) = 0;
void     (* pluginGenScancode)(Bit32u scancode) = 0;
unsigned (* pluginRegisterDMAChannel)(
                unsigned channel,
                void (* dma_read)(Bit8u *data_byte),
                void (* dma_write)(Bit8u *data_byte)
                ) = 0;
void     (* pluginDMASetDRQ)(unsigned channel, unsigned val) = 0;
unsigned (* pluginDMAGetTC)(void) = 0;

Bit32u   (* pluginHDReadHandler)(Bit32u address,
                unsigned io_len) = 0;
void     (* pluginHDWriteHandler)(Bit32u address,
                Bit32u value, unsigned io_len) = 0;
void     (* pluginVGARedrawArea)(unsigned x0, unsigned y0,
                unsigned width, unsigned height) = 0;

bx_debug_t   bx_dbg; // xxx Fix me, bochs hack
bx_options_t bx_options; // xxx Fix me, bochs hack

// xxx Fix me, bochs hack
  void
bx_panic(char *fmt, ...)
{
  va_list ap;
  
#if 0
  if (bx_logfd) {
    fprintf(bx_logfd, "panic, ");
  
    va_start(ap, fmt);
    vfprintf(bx_logfd, fmt, ap);
    va_end(ap);
    }
#endif
  fprintf(stderr, "bochs: panic, ");
  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);

  plugin_abort ();
} 

// xxx Fix me, bochs hack
  void
bx_printf(char *fmt, ...)
{
  va_list ap;

#if 0
  if (bx_logfd) {
    va_start(ap, fmt);
    vfprintf(bx_logfd, fmt, ap);
    va_end(ap); 
    }
#endif
  fprintf(stderr, "bochs: ");
  va_start(ap, fmt); 
  vfprintf(stderr, fmt, ap);
  va_end(ap);
}


  void
plugin_load (char *name, char *args)
{
    plugin_t *plugin;
    const char *plug_err;

    plugin = malloc (sizeof (plugin_t));
    if (!plugin)
    {
        perror ("malloc");
        exit (1);
    }

    plugin->name = name;
    plugin->args = args;
    plugin->initialized = 0;

    plugin->handle = dlopen (name, RTLD_LAZY);
    if (!plugin->handle)
    {
        fputs (dlerror (), stderr);
        exit (1);
    }

    plugin->init = dlsym (plugin->handle, PLUGIN_INIT);
    if ((plug_err = dlerror ()) != NULL)
    {
        fputs (plug_err, stderr);
        exit (1);
    }

    plugin->fini = dlsym (plugin->handle, PLUGIN_FINI);
    if ((plug_err = dlerror ()) != NULL)
    {
        fputs (plug_err, stderr);
        exit (1);
    }

    /* 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. */
        plugin_t *temp = plugins;

        while (temp->next)
            temp = temp->next;

        temp->next = plugin;
    }

plugin_init_one(plugin);

    return;
}


#if 0
void
plugin_init_all (void)
{
    plugin_t *plugin;

    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->init (plugin, plugin->argc, plugin->argv))
        {
            fprintf (stderr, "Plugin initialization failed for %s\n", plugin->name);
            plugin_abort();
        }

        plugin->initialized = 1;
    }

    return;
}
#endif


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->init (plugin, plugin->argc, plugin->argv))
        {
            fprintf (stderr, "Plugin initialization failed for %s\n", plugin->name);
            plugin_abort();
        }
 
        plugin->initialized = 1;
}


  plugin_t *
plugin_unload(plugin_t *plugin)
{
    int i;
    plugin_t *dead_plug;

    if (plugin->initialized)
        plugin->fini ();

    for (i=0; i<256; i++)
        plugin_free_intr (plugin, i);

    dlclose (plugin->handle);
    free (plugin->name);
    free (plugin->args);

    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));

    return;
}



  int
pluginRegisterIOReadHandler(void *thisPtr, ioReadHandler_t callback,
                            unsigned base, const char *name, unsigned len)
{
  int id;
  ioRegister_t ioHandler;

  ioHandler.thisPtr    = thisPtr;
  ioHandler.callback   = callback;
  ioHandler.base       = base;
  ioHandler.len        = len;
  ioHandler.op         = IO_IN;

  id = ioctl(filehdl, PLEX86_REGIO, &ioHandler);
  if (id == -1) {
    fprintf(stderr, "port 0x%x\n", base);
    perror ("ioctl PLEX86_REGIO(R): ");
    vm_abort();
    }
  return( id );
}


  int
pluginRegisterIOWriteHandler(void *thisPtr, ioWriteHandler_t callback,
                             unsigned base, const char *name, unsigned len)
{
  int id;
  ioRegister_t ioHandler;

  ioHandler.thisPtr    = thisPtr;
  ioHandler.callback   = callback;
  ioHandler.base       = base;
  ioHandler.len        = len;
  ioHandler.op         = IO_OUT;

  id = ioctl(filehdl, PLEX86_REGIO, &ioHandler);
  if (id == -1) {
    fprintf(stderr, "port 0x%x\n", base);
    perror ("ioctl PLEX86_REGIO(W): ");
    vm_abort();
    }
  return( id );
}


#if 0
#define BX_MAX_IRQS 16
#define BX_NO_IRQ  -1
const char *irq_handler_name[BX_MAX_IRQS];

  for (i=0; i < BX_MAX_IRQS; i++) {
    irq_handler_name[i] = NULL;
    }
#endif

  void
pluginRegisterIRQ(unsigned irq, const char *name)
{
#warning "Finish pluginRegisterIRQ"

#if 0
  if (irq >= BX_MAX_IRQS) {
    bx_panic("IO device %s registered with IRQ=%d above %u\n",
             name, irq, (unsigned) BX_MAX_IRQS-1);
    }
  if (irq_handler_name[irq]) {
    bx_panic("IRQ %u conflict, %s with %s\n", irq,
      irq_handler_name[irq], name);
    }
  irq_handler_name[irq] = name;
#endif
}

  void
pluginUnregisterIRQ(unsigned irq, const char *name)
{
#warning "Finish pluginUnregisterIRQ"
#if 0
  if (irq >= BX_MAX_IRQS) {
    bx_panic("IO device %s tried to unregister IRQ %d above %u\n",
             name, irq, (unsigned) BX_MAX_IRQS-1);
    }

  if (!irq_handler_name[irq]) {
    bx_printf("IO device %s tried to unregister IRQ %d, not registered\n",
	      name, irq);
    return;
  }

  if (strcmp(irq_handler_name[irq], name)) {
    bx_printf("IRQ %u not registered to %s but to %s\n", irq,
      name, irq_handler_name[irq]);
    return;
    }
  irq_handler_name[irq] = NULL;
#endif
}


/************************************************************************/
/* Software interrupt handling                                          */
/************************************************************************/

static struct intr_t
{
    plugin_t  *plugin;
    handler_t  handler;
    struct intr_t *next;
}
*ints[256];         /* Heads of linked lists for each int */


int
plugin_alloc_intr (plugin_t *plugin, handler_t handler, int vec)
{
    struct intr_t *intr;

    for (intr = ints[vec]; intr != NULL; intr = intr->next)
    {
        if (intr->plugin == plugin)
            return -1;
    }

    if (ints[vec] == NULL && vm_alloc_intr (vec))
        return -1;

    intr = malloc (sizeof (struct intr_t));
    intr->plugin  = plugin;
    intr->handler = handler;
    intr->next    = ints[vec];
    ints[vec]     = intr;

    return 0;
}


  void
plugin_free_intr (plugin_t *plugin, int vec)
{
    struct intr_t *intr, *dummy;

    if ((intr = ints[vec]) == NULL)
        return;

    if (intr->plugin == plugin)
    {
       dummy = intr;
       ints[vec] = intr->next;
       free (dummy);
    }
    else
        for (; intr != NULL; intr = intr->next)
        {
            if (intr->next == NULL)
                break;

            if (intr->next->plugin == plugin)
            {
                dummy = intr->next;
                intr->next = intr->next->next;
                free (dummy);

                break;
            }
        }

    if (ints[vec] == NULL)
        vm_release_intr (vec);

    return;
}


  int
plugin_emulate_int(int vec)
{
    struct intr_t *intr;
    int reflect = 0;

    for (intr = ints[vec]; intr != NULL; intr = intr->next)
    {
        if (intr->handler (EVT_INT, vec, 0, 0, NULL))
            reflect = 1;
    }

    return reflect;
}



/************************************************************************/
/* VM control                                                           */
/************************************************************************/

void
plugin_abort (void)
{
    vm_abort ();
    return;
}



/************************************************************************/
/* Plugin pending                                                       */
/************************************************************************/

/*
 * NOTE:  This code was explicitly written to be reentrant using atomic
 *        XCHG, because it will usually be called from signal handlers and
 *        the like.  None of the other plugin functions are reentrant, so
 *        a plugin wanting to perform plugin operations after reception
 *        of a signal should always use these functions to pend for CPU
 *        time.
 */


/************************************************************************/
/*                                                                      */
/************************************************************************/

void (*memMapFunct)(Bit32u, unsigned, unsigned, Bit32u *) = NULL;

  void
plugin_register_mem_map_IO( void (*f)(Bit32u, unsigned, unsigned, Bit32u *),
  Bit32u range0, Bit32u range1)
{
  memMapFunct = f;
}


/************************************************************************/
/* Timing control                                                       */
/************************************************************************/

static void (*save_funct)(Bit64u) = NULL;
static void (*periodic_funct)(void) = NULL;


  void
plugin_register_elapsed(void (*funct)(Bit64u))
{
  save_funct = funct;
}

  void
plugin_register_periodic(void (*funct)(void))
{
  periodic_funct = funct;
}


  void
plugin_call_elapsed( Bit64u elapsed )
{
  if (save_funct)
    save_funct(elapsed);
}

  void
plugin_handle_periodic(void)
{
  static Bit64u prev_user_usec = 0;
  Bit64u new_user_usec;
  volatile extern Bit64u user_time_usec;

  if (!periodic_funct) return;

#warning "fix: need exclusive access to variable used by async handler"
  new_user_usec = user_time_usec;
  if ( (new_user_usec - prev_user_usec) >= 500000 ) {
    prev_user_usec = new_user_usec;
    periodic_funct();
    }
}



/************************************************************************/
/* Plugin system: plex86 startup function                               */
/************************************************************************/

#define ZERO_ARRAY(a)  memset (a, 0, sizeof(a))

  void
plugin_startup(void)
{
  ZERO_ARRAY(ints);

  /* Register builtin Write-Cache functions, so we have some defaults
   * until overloaded by plugins
   */
  pluginRegisterWriteCache(builtinWCLSeek, builtinWCRead, builtinWCWrite,
                           builtinWCCommit);
  pluginGetCMOSReg   = builtinGetCMOSReg;
  pluginSetCMOSReg   = builtinSetCMOSReg;
  pluginCMOSChecksum = builtinCMOSChecksum;
  pluginMouseMotion  = builtinMouseMotion;
  pluginGenScancode  = builtinGenScancode;
  pluginRegisterDMAChannel = builtinRegisterDMAChannel;
  pluginDMASetDRQ    = builtinDMASetDRQ;
  pluginDMAGetTC     = builtinDMAGetTC;

  pluginHDReadHandler  = builtinHDReadHandler;
  pluginHDWriteHandler = builtinHDWriteHandler;

  pluginVGARedrawArea  = builtinVGARedrawArea;
}

  Bit32u
builtinGetCMOSReg(unsigned reg)
{
  fprintf(stderr, "builtinGetCMOSReg called, no CMOS plugin loaded?\n");
  vm_abort();
  return 0;
}

  void
builtinSetCMOSReg(unsigned reg, Bit32u val)
{
  fprintf(stderr, "builtinSetCMOSReg called, no CMOS plugin loaded?\n");
  vm_abort();
}

  void
builtinCMOSChecksum(void)
{
  fprintf(stderr, "builtinCMOSChecksum called, no CMOS plugin loaded?\n");
  vm_abort();
}

  void
builtinMouseMotion(int d_x, int d_y, unsigned button_state)
{
  fprintf(stderr, "builtinMouseMotion called, not overloaded by keyboard"
                  " plugin?\n");
  vm_abort();
}

  void
builtinGenScancode(Bit32u scancode)
{
  fprintf(stderr, "builtinGenScancode called, not overloaded by keyboard"
                  " plugin?\n");
  vm_abort();
}

  unsigned
builtinRegisterDMAChannel(
  unsigned channel,
  void (* dma_read)(Bit8u *data_byte),
  void (* dma_write)(Bit8u *data_byte)
  )
{
  fprintf(stderr, "builtinRegisterDMAChannel called, not overloaded by DMA"
                  " plugin?\n");
  vm_abort();
  return 0;
}

  void
builtinDMASetDRQ(unsigned channel, unsigned val)
{
  fprintf(stderr, "builtinDMASetDRQ( called, not overloaded by DMA"
                  " plugin?\n");
  vm_abort();
}

  unsigned
builtinDMAGetTC(void)
{
  fprintf(stderr, "builtinDMAGetTC called, not overloaded by DMA"
                  " plugin?\n");
  vm_abort();
  return 0;
}

  Bit32u
builtinHDReadHandler(Bit32u address, unsigned io_len)
{
  fprintf(stderr, "builtinHDReadHandler called, HD plugin not loaded?"
                  " plugin?\n");
  vm_abort();
  return 0;
}

  void
builtinHDWriteHandler(Bit32u address, Bit32u value, unsigned io_len)
{
  fprintf(stderr, "builtinHDWriteHandler called, HD plugin not loaded?"
                  " plugin?\n");
  vm_abort();
}

  void
builtinVGARedrawArea(unsigned x0, unsigned y0,
                     unsigned width, unsigned height)
{
}

  Bit64u
builtinWCLSeek(int fd, Bit64u off64, int whence)
{
  return( lseek(fd, (off_t) off64, whence) );
}

  unsigned
builtinWCRead(int fd, void *buffer, unsigned len)
{
  return( read(fd, buffer, len) );
}

  unsigned
builtinWCWrite(int fd, void *buffer, unsigned len)
{
  return( write(fd, buffer, len) );
}

  void
builtinWCCommit(int fd)
{
  fprintf(stderr, "builtin Write-Cache Commit (nop)\n");
}


// xxx Fix these
int plugin_emulate_inport(int port, int op_size, int count, void *loc)
{
  fprintf(stderr, "plugin_emulate_inport:\n");
  vm_abort();
}

int plugin_emulate_outport(int port, int op_size, int count, void *loc)
{
  fprintf(stderr, "plugin_emulate_outport:\n");
  vm_abort();
}

  void
pluginTriggerIRQ(unsigned irq)
{
  int ret;
  irqMsg_t irqMsg;

  irqMsg.irq = irq;
  irqMsg.on  = 1;

  ret = ioctl(filehdl, PLEX86_IRQ, &irqMsg);
  if (ret == -1) {
    perror ("ioctl PLEX86_IRQ: ");
    vm_abort();
    }
}

  void
pluginUntriggerIRQ(unsigned irq)
{
  int ret;
  irqMsg_t irqMsg;

  irqMsg.irq = irq;
  irqMsg.on  = 0;

  ret = ioctl(filehdl, PLEX86_IRQ, &irqMsg);
  if (ret == -1) {
    perror ("ioctl PLEX86_IRQ: ");
    vm_abort();
    }
}

  void
pluginSetHRQ(unsigned val)
{
  pluginHRQ = val;
}

  void
pluginSetHRQHackCallback( void (*callback)(void) )
{
  pluginHRQHackCallback = callback;
}

  void
pluginResetSignal(unsigned sig)
{
  fprintf(stderr, "pluginResetSignal: unimplemented\n");
  vm_abort();
}