summaryrefslogtreecommitdiff
path: root/boot/ux.c
blob: 7239762ccc4b42317f458fc4a13ade22c8348037 (plain)
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
/* Hacks to make boot work under UX

   Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   The GNU Hurd 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <mach.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <pthread.h>

#include "ux.h"

#if 0
static int (* const _sc)(int, ...) = &syscall;
int _sc_print = 1;

#define syscall(num, args...) \
 ({ int _rv, _num = (num), _pr = _sc_print; \
    _sc_print = 0; \
    if (_pr) printf ("syscall (%d) start\r\n", _num); \
    _rv = (*_sc) (_num , ##args); \
    if (_pr) printf ("syscall (%d) end\r\n", _num); \
    _sc_print = _pr; \
    _rv; \
   })
#endif

extern void __mach_init ();
void	(*mach_init_routine)() = __mach_init;

/* These will prevent the Hurd-ish versions from being used */

struct free_reply_port
{
  mach_port_t port;
  struct free_reply_port *next;
};
static struct free_reply_port *free_reply_ports = NULL;
static pthread_spinlock_t free_reply_ports_lock = PTHREAD_SPINLOCK_INITIALIZER;

mach_port_t __mig_get_reply_port ()
{
  pthread_spin_lock (&free_reply_ports_lock);
  if (free_reply_ports == NULL)
    {
      pthread_spin_unlock (&free_reply_ports_lock);
      return __mach_reply_port ();
    }
  else
    {
      struct free_reply_port *frp = free_reply_ports;
      mach_port_t reply_port = frp->port;
      free_reply_ports = free_reply_ports->next;
      pthread_spin_unlock (&free_reply_ports_lock);
      free (frp);
      return reply_port;
    }
}
mach_port_t mig_get_reply_port ()
{
  return __mig_get_reply_port ();
}
void __mig_put_reply_port (mach_port_t port)
{
  struct free_reply_port *frp = malloc (sizeof (struct free_reply_port));
  frp->port = port;
  pthread_spin_lock (&free_reply_ports_lock);
  frp->next = free_reply_ports;
  free_reply_ports = frp;
  pthread_spin_unlock (&free_reply_ports_lock);
}
void mig_put_reply_port (mach_port_t port)
{
  __mig_put_reply_port (port);
}
void __mig_dealloc_reply_port (mach_port_t port)
{
  mach_port_mod_refs (__mach_task_self (), port,
		      MACH_PORT_RIGHT_RECEIVE, -1);
}
void mig_dealloc_reply_port (mach_port_t port)
{
  __mig_dealloc_reply_port (port);
}
void __mig_init (void *stack) {}
void mig_init (void *stack) {}

int
task_by_pid (int pid)
{
  return syscall (-33, pid);
}

int
write (int fd,
       const void *buf,
       int buflen)
{
  return syscall (4, fd, buf, buflen);
}

int
read (int fd,
      void *buf,
      int buflen)
{
  return syscall (3, fd, buf, buflen);
}

int
open (const char *name,
      int flags,
      int mode)
{
  return syscall (5, name, flags, mode);
}

int
uxfstat (int fd, struct uxstat *buf)
{
  return syscall (62, fd, buf);
}

int
close (int fd)
{
  return syscall (6, fd);
}

int
lseek (int fd,
       int off,
       int whence)
{
  return syscall (19, fd, off, whence);
}

int
uxexit (int code)
{
  return syscall (1, code);
}

int
getpid ()
{
  return syscall (20);
}

int
ioctl (int fd, int code, void *buf)
{
  return syscall (54, fd, code, buf);
}

int
sigblock (int mask)
{
  return syscall (109, mask);
}

int
sigsetmask (int mask)
{
  return syscall (110, mask);
}

int
sigpause (int mask)
{
  return syscall (111, mask);
}


#if 0
void
sigreturn ()
{
  asm volatile ("movl $0x67,%eax\n"
		"lcall $0x7, $0x0\n"
		"ret");
}

void
_sigreturn ()
{
  asm volatile ("addl $0xc, %%esp\n"
		"call %0\n"
		"ret"::"m" (sigreturn));
}

int
sigvec (int sig, struct sigvec *vec, struct sigvec *ovec)
{
  asm volatile ("movl $0x6c,%%eax\n"
		"movl %0, %%edx\n"
		"orl $0x80000000, %%edx\n"
		"lcall $0x7,$0x0\n"
		"ret"::"g" (_sigreturn));
}
#else
int sigvec ();
#endif

void get_privileged_ports (mach_port_t *host_port, mach_port_t *device_port)
{
  *host_port = task_by_pid (-1);
  *device_port = task_by_pid (-2);
}

/* A *really* stupid printf that only understands %s & %d.  */
int
printf (const char *fmt, ...)
{
  va_list ap;
  const char *p = fmt, *q = p;

  void flush (const char *new)
    {
      if (p > q)
	write (1, q, p - q);
      q = p = new;
    }

  va_start (ap, fmt);
  while (*p)
    if (*p == '%' && p[1] == 's')
      {
	char *str = va_arg (ap, char *);
	flush (p + 2);
	write (1, str, strlen (str));
      }
    else if (*p == '%' && p[1] == 'd')
      {
	int i = va_arg (ap, int);
	char rbuf[20], *e = rbuf + sizeof (rbuf), *b = e; 

	if (i == 0)
	  *--b = '0';
	else
	  while (i)
	    {
	      *--b = i % 10 + '0';
	      i /= 10;
	    }

	flush (p + 2);
	write (1, b, e - b);
      }
    else
      p++;
  va_end (ap);

  flush (0);

  return 0;
}

static struct sgttyb term_sgb;
static int localbits;

void
init_termstate ()
{
  struct sgttyb sgb;
  int bits;
  ioctl (0, TIOCGETP, &term_sgb);
  ioctl (0, TIOCLGET, &localbits);
  /* Enter raw made.  Rather than try and interpret these bits,
     we just do what emacs does in .../emacs/src/sysdep.c for
     an old style terminal driver. */
  bits = localbits | LDECCTQ | LLITOUT | LPASS8 | LNOFLSH;
  ioctl (0, TIOCLSET, &bits);
  sgb = term_sgb;
  sgb.sg_flags &= ~ECHO;
  sgb.sg_flags |= RAW | ANYP;
  ioctl (0, TIOCSETN, &sgb);
}

void
restore_termstate ()
{
  ioctl (0, TIOCLSET, &localbits);
  ioctl (0, TIOCSETN, &term_sgb);
}