xsupport.c 2.97 KB
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */

/****************************************************************
 * $File$
 * 
 * $Author: blythe $
 * $Date: 2002/05/29 01:09:09 $
 *****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include "xsupport.h"
#include "checkpoint.h"
#include "startup.h"
#include "sim.h"
#include "simutil.h"

static int framebuf_fd, eventq_fd;
static CptCallback FrameCheckpointCB;
static CptCallback EventQCheckpointCB;

static int FrameCheckpointCB(CptDescriptor *cptd);
static int EventQCheckpointCB(CptDescriptor *cptd);

/*****************************************************************
 * This is added for supporting an X-windows session on top of
 * simos. The event queue needs a little bit of work.
 *****************************************************************/
void
InitXSupport(int restoringCpt)
{
   char *addr;
   int framebuf_fd;

   Simcpt_Register("framebuffer", FrameCheckpointCB, ALL_CPUS);
   Simcpt_Register("eventqueue", EventQCheckpointCB, ALL_CPUS);

   /*
    * Create a region to store the frame buffer
    */
   framebuf_fd = open("frame.simos", O_RDWR | O_CREAT, 0700);
   if (framebuf_fd < 0) {
      perror("Opening frame.simos");
      Sim_Error("Simprom_start can't start");
   }
   
   addr = (char *)mmap((char *)SIMFRAMEBUFFER,
                       SIMFRAMEBUFFER_SZ,
                       PROT_WRITE,
                       MAP_SHARED, framebuf_fd, 0);

   if (addr != (char *)SIMFRAMEBUFFER) {
      perror("mapping frame buffer");
      Sim_Error("Can't map frame buffer");
   }
   
   if (restoringCpt) {
      Simcpt_Restore("framebuffer");
   } else {
      bzero((char *)SIMFRAMEBUFFER, SIMFRAMEBUFFER_SZ);
   }
   close(framebuf_fd);
       
   /*
    * Create a region to store the input event queue to the simulated x server
    */
   eventq_fd = open("event.simos", O_RDWR | O_CREAT, 0777);
   if (eventq_fd < 0) {
      perror("Opening event.simos");
      Sim_Error("Simprom_start can't start");
   }

   addr = (char *)mmap((char *)SIMEVENTQ, SIMEVENTQ_SZ,
                       PROT_WRITE, MAP_SHARED, eventq_fd, 0);

   if (addr != (char *) SIMEVENTQ) {
      perror("mapping event queue");
      Sim_Error("Can't map event queue");
   }
       
   if (restoringCpt) {
      Simcpt_Restore("eventqueue");
   }
   bzero((char *)SIMEVENTQ, SIMEVENTQ_SZ);
   close(eventq_fd);
}

static int 
FrameCheckpointCB(CptDescriptor *cptd)
{
  Simcpt_CptBlock(cptd, "framebuffer", NO_INDEX, NO_INDEX,
                  (caddr_t)SIMFRAMEBUFFER, SIMFRAMEBUFFER_SZ, framebuf_fd);
  return 0;
}


static int 
EventQCheckpointCB(CptDescriptor *cptd)
{
  Simcpt_CptBlock(cptd, "eventqueue", NO_INDEX, NO_INDEX,
                  (caddr_t)SIMEVENTQ, SIMEVENTQ_SZ, eventq_fd);
  return 0;

}