summaryrefslogtreecommitdiff
path: root/mit-pthreads/machdep/engine-i386-sco-3.2v5.c
blob: 95f0d757585745d711e32cbed110cd41bace31e8 (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
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072

/* ==== machdep.c ============================================================
 * Copyright (c) 1995 by Chris Provenzano, proven@mit.edu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *  This product includes software developed by Chris Provenzano.
 * 4. The name of Chris Provenzano may not be used to endorse or promote
 *      products derived from this software without specific prior written
 *      permission.
 *
 * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Description : Machine dependent functions for SCO3.2v5 on i386
 *
 *	1.00 96/11/21 proven
 *      -Started coding this file.
 */

#ifndef lint
static const char rcsid[] = "engine-i386-freebsd-2.0.c,v 1.1 1995/03/01 01:21:20 proven Exp";
#endif

#include <pthread.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <sys/stat.h>
#include <stropts.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <sys/unistd.h>
#include <sys/utsname.h>
#include <sys/sysi86.h>

void machdep_sys_abort(char*fname,int lineno)

{
	char buf[128];

	sprintf(buf,"panic: %s => %d\n", fname, lineno);
	machdep_sys_write(1, buf, strlen(buf));
	abort();
}

#if 0
int setitimer(int which, struct itimerval* value, struct itimerval* ovalue)

{
	register int ret;
	if ((ret = machdep_sys_setitimer(which,value,ovalue))<0) {
	   errno = -ret;
	   return -1;
        }
	else {
	   return 0;
	}
}
#endif

/* ==========================================================================
 * machdep_save_state()
 */
int machdep_save_state(void)
{
    return(setjmp(pthread_run->machdep_data.machdep_state));
}

/* ==========================================================================
 * machdep_save_state()
 */
int machdep_save_float_state(struct pthread * pthread)
{
	char * fdata = (char *)pthread->machdep_data.machdep_float_state;
	__asm__ ("fsave %0"::"m" (*fdata));
}

/* ==========================================================================
 * machdep_restore_state()
 */
void machdep_restore_state(void)
{
    longjmp(pthread_run->machdep_data.machdep_state, 1);
}

/* ==========================================================================
 * machdep_restore_float_state()
 */
int machdep_restore_float_state(void)
{
	char * fdata = (char *)pthread_run->machdep_data.machdep_float_state;
	__asm__ ("frstor %0"::"m" (*fdata));
}

/* ==========================================================================
 * machdep_set_thread_timer()
 */
void machdep_set_thread_timer(struct machdep_pthread *machdep_pthread)
{
    if (machdep_sys_setitimer(ITIMER_VIRTUAL, &(machdep_pthread->machdep_timer), NULL)) {
        PANIC();
    }
}

/* ==========================================================================
 * machdep_unset_thread_timer()
 */
void machdep_unset_thread_timer(struct machdep_pthread *machdep_pthread)
{
    struct itimerval zeroval = { { 0, 0 }, { 0, 0 } };
	int ret;

	if (machdep_pthread) {
    	ret = machdep_sys_setitimer(ITIMER_VIRTUAL, &zeroval, 
		  &(machdep_pthread->machdep_timer));
	} else {
    	ret = machdep_sys_setitimer(ITIMER_VIRTUAL, &zeroval, NULL); 
    }

	if (ret) {
       	PANIC();
	}
}

/* ==========================================================================
 * machdep_pthread_cleanup()
 */
void *machdep_pthread_cleanup(struct machdep_pthread *machdep_pthread)
{
    return(machdep_pthread->machdep_stack);
}

/* ==========================================================================
 * machdep_pthread_start()
 */
void machdep_pthread_start(void)
{
	context_switch_done();
	pthread_sched_resume();

    /* Run current threads start routine with argument */
    pthread_exit(pthread_run->machdep_data.start_routine
      (pthread_run->machdep_data.start_argument));

    /* should never reach here */
    PANIC();
}

/* ==========================================================================
 * __machdep_stack_free()
 */ 
void __machdep_stack_free(void * stack)
{   
    free(stack);
}

/* ==========================================================================
 * __machdep_stack_alloc()
 */
void * __machdep_stack_alloc(size_t size)
{
    void * stack;
   
    return(malloc(size));
}  

/* ==========================================================================
 * __machdep_pthread_create()
 */
void __machdep_pthread_create(struct machdep_pthread *machdep_pthread,
  void *(* start_routine)(), void *start_argument, 
  long stack_size, long nsec, long flags)
{
    machdep_pthread->start_routine = start_routine;
    machdep_pthread->start_argument = start_argument;

    machdep_pthread->machdep_timer.it_value.tv_sec = 0;
    machdep_pthread->machdep_timer.it_interval.tv_sec = 0;
    machdep_pthread->machdep_timer.it_interval.tv_usec = 0;
    machdep_pthread->machdep_timer.it_value.tv_usec = nsec / 1000;

    setjmp(machdep_pthread->machdep_state);
    /*
     * Set up new stact frame so that it looks like it
     * returned from a longjmp() to the beginning of
     * machdep_pthread_start().
     */
    machdep_pthread->machdep_state[JB_PC] = (int)machdep_pthread_start;

    /* Stack starts high and builds down. */
    machdep_pthread->machdep_state[JB_SP] =
      (int)machdep_pthread->machdep_stack + stack_size;
}

/* ==========================================================================
 * machdep_sys_wait3() 
 */
machdep_sys_wait3(int * b, int c, int * d)
{
#if 0
        return(machdep_sys_wait4(0, b, c, d));
#else
	return -ENOSYS;
#endif
}
 
/* ==========================================================================
 * machdep_sys_fstat()
 */
machdep_sys_fstat(int f, struct stat* b)
{
	return machdep_sys_fxstat(0x33, f, b);
}

/* ==========================================================================
 * machdep_sys_dup2()
 */
machdep_sys_dup2(int a, int b)
{
	machdep_sys_close(b);
	return machdep_sys_fcntl(a, F_DUPFD, b);
}

/* ==========================================================================
 * machdep_sys_getdtablesize()
 */
machdep_sys_getdtablesize()

{
	register int ret;
	if ((ret = machdep_sys_sysconf(_SC_OPEN_MAX))<0)
	   PANIC();
	return ret;
}

/* ==========================================================================
 * machdep_sys_fchown()
 */
machdep_sys_fchown(int fd,uid_t owner,gid_t group)

{
    return -ENOSYS;
}

/* ==========================================================================
 * machdep_sys_fchmod()
 */
machdep_sys_fchmod(int fd,mode_t mode)

{
    return -ENOSYS;
}

/* ==========================================================================
 * machdep_sys_getdirentries()
 */
int machdep_sys_getdirentries(int fd, char * buf, int len, int * seek)
{
	return(machdep_sys_getdents(fd, buf, len));
}

/* ==========================================================================
 * SCO Socket calls are a bit different
 * ==========================================================================
 * machdep_sys_socket()
 */
int machdep_sys_socket(int domain, int type, int protocol)
{
	register int s, fd, ret;	
	struct socksysreq req;

	if ((s = machdep_sys_open("/dev/socksys", 0))<0)
           return s;

	req.args[0] = SO_SOCKET;
	req.args[1] = (int)domain;
	req.args[2] = (int)type;
	req.args[3] = (int)protocol;
	if ((fd = machdep_sys_ioctl(s, SIOCSOCKSYS, &req))<0) {
           machdep_sys_close(s);
	   return fd;
        }

	if ((ret=machdep_sys_dup2(fd, s))<0) {
	   machdep_sys_close(fd);
	   return ret;
	}

	machdep_sys_close(fd);
	return s;

}

/* ==========================================================================
 * machdep_sys_accept()
 */
int machdep_sys_accept(int s, struct sockaddr * b, int * c)
{
	struct socksysreq req;

	req.args[0] = SO_ACCEPT;
	req.args[1] = (int)s;
	req.args[2] = (int)b;
	req.args[3] = (int)c;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_bind()
 */
int machdep_sys_bind(int s, const struct sockaddr * b, int c)
{
	struct socksysreq req;

	req.args[0] = SO_BIND;
	req.args[1] = (int)s;
	req.args[2] = (int)b;
	req.args[3] = (int)c;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_connect()
 */
int machdep_sys_connect(int s, const struct sockaddr * b, int c)
{
	struct socksysreq req;

	req.args[0] = SO_CONNECT;
	req.args[1] = (int)s;
	req.args[2] = (int)b;
	req.args[3] = (int)c;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_listen()
 */
int machdep_sys_listen(int s, int backlog)
{
	struct socksysreq req;

	req.args[0] = SO_LISTEN;
	req.args[1] = (int)s;
	req.args[2] = (int)backlog;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_shutdown()
 */
int machdep_sys_shutdown(int s, int b)
{
    struct socksysreq req;

    req.args[0] = SO_SHUTDOWN;
    req.args[1] = (int)s;
    req.args[2] = (int)b;

    return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_getsockopt()
 */
int machdep_sys_getsockopt(int s, int b, int c, char *d, int *e)
{
	struct socksysreq req;

	req.args[0] = SO_GETSOCKOPT;
	req.args[1] = (int)s;
	req.args[2] = (int)b;
	req.args[3] = (int)c;
	req.args[4] = (int)d;
	req.args[5] = (int)e;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_setsockopt()
 */
int machdep_sys_setsockopt(int s, int b, int c, char *d, int e)
{
	struct socksysreq req;

	req.args[0] = SO_SETSOCKOPT;
	req.args[1] = (int)s;
	req.args[2] = (int)b;
	req.args[3] = (int)c;
	req.args[4] = (int)d;
	req.args[5] = (int)e;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_getpeername()
 */
int machdep_sys_getpeername(int s, struct sockaddr *b, int *c)
{
	struct socksysreq req;

	req.args[0] = SO_GETPEERNAME;
	req.args[1] = (int)s;
	req.args[2] = (int)b;
	req.args[3] = (int)c;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_send()
 */
int machdep_sys_send(int s, char *b, int c, int d)
{
	struct socksysreq req;

	req.args[0] = SO_SEND;
	req.args[1] = (int)s;
	req.args[2] = (int)b;
	req.args[3] = (int)c;
	req.args[4] = (int)d;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_sendto()
 */
int machdep_sys_sendto(int s, char *b, int c, int d,
  struct sockaddr *e, int f)
{
	struct socksysreq req;

	req.args[0] = SO_SENDTO;
	req.args[1] = (int)s;
	req.args[2] = (int)b;
	req.args[3] = (int)c;
	req.args[4] = (int)d;
	req.args[5] = (int)e;
	req.args[6] = (int)f;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_recv()
 */
int machdep_sys_recv(int s, char *b, int c, int d)
{
	struct socksysreq req;

	req.args[0] = SO_RECV;
	req.args[1] = (int)s;
	req.args[2] = (int)b;
	req.args[3] = (int)c;
	req.args[4] = (int)d;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_recvfrom()
 */
int machdep_sys_recvfrom(int s, char *buf, int len, int flags,
  struct sockaddr *from, int *fromlen)
{
	struct socksysreq req;

	req.args[0] = SO_RECVFROM;
	req.args[1] = (int)s;
	req.args[2] = (int)buf;
	req.args[3] = (int)len;
	req.args[4] = (int)flags;
	req.args[5] = (int)from;
	req.args[6] = (int)fromlen;

	return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

/* ==========================================================================
 * machdep_sys_socketpair()
 */
int machdep_sys_socketpair(int d, int type, int protocol, int sv[2])

{
	register int s1, s2;
	register int ret;
	struct socksysreq req;

	if (d != AF_UNIX)
	   return -EPROTONOSUPPORT;
	if ((s1=machdep_sys_socket(d,type,protocol))<0) {
	   return s1;
	}
	if ((s2=machdep_sys_socket(d,type,protocol))<0) {
	   machdep_sys_close(s1);
	   return s2;
	}
	req.args[0] = SO_SOCKPAIR;
	req.args[1] = s1;
	req.args[2] = s2;
	if ((ret=machdep_sys_ioctl(s1,SIOCSOCKSYS,&req))<0) {
	   machdep_sys_close(s1);
	   machdep_sys_close(s2);
	   return ret;
	}
	sv[0] = s1;
	sv[1] = s2;
	return 0;
}

/* ==========================================================================
 * machdep_sys_getsockname()
 */
int machdep_sys_getsockname(int s, char * b, int * c)
{
    struct socksysreq req;

    req.args[0] = SO_GETSOCKNAME;
    req.args[1] = (int)s;
    req.args[2] = (int)b;
    req.args[3] = (int)c;

    return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

int machdep_sys_sendmsg(int s, const struct msghdr *msg, int flags)

{
    struct socksysreq req;

    req.args[0] = SO_SENDMSG;
    req.args[1] = (int)s;
    req.args[2] = (int)msg;
    req.args[3] = (int)flags;

    return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

int machdep_sys_recvmsg(int s, struct msghdr *msg, int flags)

{
    struct socksysreq req;

    req.args[0] = SO_RECVMSG;
    req.args[1] = (int)s;
    req.args[2] = (int)msg;
    req.args[3] = (int)flags;

    return(machdep_sys_ioctl(s, SIOCSOCKSYS, &req));
}

u_short ntohs(u_short n)

{
    union {
       unsigned char u_nc[4];
       u_short u_ns;
    } ns;
    register unsigned char* p = &ns.u_nc[0];

    ns.u_ns = n;
    return (p[0]<<8)|p[1];
}

u_short htons(u_short h)

{
    union {
       unsigned char u_nc[2];
       u_short u_ns;
    } ns;
    register unsigned char* p = &ns.u_nc[0];
    p[0] = (h>>8)&0xFF;
    p[1] = (h&0xFF);
    return ns.u_ns;
}


u_long ntohl(u_long n)

{
    union {
       unsigned char u_nc[4];
       u_long u_nl;
    } nl;
    register unsigned char* p = &nl.u_nc[0];

    nl.u_nl = n;
    return (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3];
}

u_long htonl(u_long h)

{
    union {
       unsigned char u_nc[4];
       u_long u_nl;
    } nl;
    register unsigned char* p = &nl.u_nc[0];
    p[0] = (h>>24)&0xFF;
    p[1] = (h>>16)&0xFF;
    p[2] = (h>>8)&0xFF;
    p[3] = (h&0xFF);
    return nl.u_nl;
}

int getdomainname(char* domain,int len)

{
	/* edi = len */
    struct socksysreq req;
    register int ret, fd;
    if (len>MAXHOSTNAMELEN)
       len = MAXHOSTNAMELEN;

    if ((fd = machdep_sys_open("/dev/socksys", 0)) < 0)
	 return fd;

    req.args[0] = SO_GETIPDOMAIN;
    req.args[1] = (int)domain;
    req.args[2] = (int)len;
    if((ret=machdep_sys_ioctl(fd, SIOCSOCKSYS, &req))<0) {
       machdep_sys_close(fd);
       return ret;
    }

    machdep_sys_close(fd);
    domain[len-1] = '\0';
    return 0;
}

int gethostname(char* name, int namelen)

{
	struct utsname uts;
	register int ret, len;
	char domain[MAXHOSTNAMELEN+1];

	if (name==NULL)
	   return -EFAULT;
	if ((ret=machdep_sys_uname(&uts))<0)
 	   return ret;
	if (namelen<(len=strlen(uts.nodename)))
	   return -EFAULT;
	strncpy(name,uts.nodename,len);
	if (namelen>len)
	   name[len] = '\0';
	if ((ret=getdomainname(domain, namelen - len))<0)
	   return ret;
	if (domain[0]=='\0')
	   return 0;
	if (len + strlen(domain) + 2 > namelen)
	   return -EFAULT;
	strcat(name, ".");
	strcat(name, domain);
	return 0;
}

int gettimeofday(struct timeval* tp, struct timezone* tz)

{
	register int ret;
	if ((ret = machdep_sys_gettimeofday(tp, NULL))<0) {
	   errno = -ret;
	   return -1;
        }
	else {
	   return 0;
	}
}

int kill(pid_t pid, int signo)

{
	register int ret;
	if ((ret = machdep_sys_kill(pid,signo))<0) {
	   errno = -ret;
	   return -1;
        }
	else {
	   return 0;
	}
}

typedef void (*signal_t(int signo, void (*func)(int)))(int);

signal_t* _libc_signal = NULL;

void (*signal(int signo, void (*func)(int)))(int)

{
	int ret;
	void (*oldfunc)(int);
	extern void (*machdep_sys_signal(int signo, void (*func)(int),int* r))(int);
	if (_libc_signal!=NULL)
	   return (*_libc_signal)(signo, func);

	oldfunc = machdep_sys_signal(signo, func, &ret);
	if (ret!=0) {
	   errno = ret;
	   return SIG_ERR;
	}
	else {
	   return oldfunc;
	}
}

int (*_libc_sigaction)(int ,const struct sigaction *, struct sigaction *) = NULL;
int sigaction(int sig,const struct sigaction *act, struct sigaction *oact)

{
	register int ret;
	if (_libc_sigaction!=NULL)
	   return (*_libc_sigaction)(sig,act,oact);
	if ((ret = machdep_sys_sigaction(sig,act,oact))<0) {
	   errno = -ret;
	   return -1;
	}
	else {
	   return 0;
	}
}

int (*_libc_sigprocmask)(int, const sigset_t *, sigset_t *) = NULL;

int sigprocmask(int how, const sigset_t *set, sigset_t * oset)

{
	register int ret;
	if (_libc_sigprocmask!=NULL)
	   return (*_libc_sigprocmask)(how,set,oset);
	if ((ret = machdep_sys_sigprocmask(how,set,oset))<0) {
	   errno = -ret;
	   return -1;
	}
	else {
	   return 0;
	}
}

int (*_libc_sigsuspend)(const sigset_t *) = NULL;

int sigsuspend(const sigset_t *set)
{
	register int ret;
	if (_libc_sigsuspend!=NULL)
	   return (*_libc_sigsuspend)(set);
	if ((ret = machdep_sys_sigsuspend(set))<0) {
	   errno = -ret;
	   return -1;
	}
	else {
	   return 0;
	}
}

int _sigrelse(sig)
int sig;

{
    sigset_t mask;
    sigemptyset(&mask);
    sigaddset(&mask, sig);
    return sigprocmask(SIG_UNBLOCK,&mask,NULL);
}

int _sighold(sig)
int sig;

{
    sigset_t mask;
    sigemptyset(&mask);
    sigaddset(&mask, sig);
    return sigprocmask(SIG_BLOCK,&mask,NULL);
}

void (*sigset(int sig, void (*func)(int)))(int)
{
    return signal(sig, func);
}


int (*_libc_getmsg)(int , struct strbuf *, struct strbuf *, int *) = NULL;

int getmsg(int fd, struct strbuf * ctlptr, struct strbuf * dataptr,
  int * flags)
{
	register int ret;
	if (_libc_getmsg != NULL)
	   return (*_libc_getmsg)(fd,ctlptr,dataptr,flags);
	else if ((ret=machdep_sys_getmsg(fd,ctlptr,dataptr,flags))<0) {
	   errno = -ret;
	   return -1;
	}
	else
	   return ret;
}

int (*_libc_putmsg)(int , const struct strbuf *, const struct strbuf *, int) = NULL;

int putmsg(int fd, const struct strbuf * ctlptr, const struct strbuf * dataptr,
  int flags)
{
	register int ret;
	if (_libc_putmsg != NULL)
	   return (*_libc_putmsg)(fd,ctlptr,dataptr,flags);
	else if ((ret=machdep_sys_putmsg(fd,ctlptr,dataptr,flags))<0) {
	   errno = -ret;
	   return -1;
	}
	else
	   return ret;
}

int ftime(struct timeb* tp)

{
	register int ret;
	if ((ret=machdep_sys_ftime(tp))<0) {
		errno = -ret;
		return NOTOK;
	}
	return 0;
}

int getpagesize()

{
	register int ret;
#if 0
	if ((ret = machdep_sys_sysconf(_SC_PAGE_SIZE))<0) {
	   PANIC();
	   SET_ERRNO(-ret);
	   return -1;
	}
	else {
	   return 0;
	}
#else
	return PAGESIZE;
#endif
}

static 	pthread_mutex_t machdep_mutex = 
{ MUTEX_TYPE_COUNTING_FAST, PTHREAD_QUEUE_INITIALIZER, \
	 NULL, SEMAPHORE_CLEAR, { NULL }, MUTEX_FLAGS_INITED };

static 	pthread_mutex_t malloc_mutex = 
{ MUTEX_TYPE_COUNTING_FAST, PTHREAD_QUEUE_INITIALIZER, \
	 NULL, SEMAPHORE_CLEAR, { NULL }, MUTEX_FLAGS_INITED };

struct stdlock {
    volatile long init;
    pthread_mutex_t* mutex;
};

static void machdep_stdinitlock(struct stdlock* lock)

{
	if (lock==0) PANIC();
	pthread_mutex_lock(&machdep_mutex);
	if (!lock->init) {
	   register pthread_mutex_t* mutex;
	   pthread_mutexattr_t attr;

	   lock->init = 1;
	   lock->mutex = &machdep_mutex;
	   mutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
	   pthread_mutexattr_init (&attr);
	   pthread_mutexattr_settype (&attr, MUTEX_TYPE_COUNTING_FAST);
	   pthread_mutex_init(mutex, &attr);
	   lock->mutex = mutex;
	}
	pthread_mutex_unlock(&machdep_mutex);
}

void machdep_stdlock(struct stdlock* lock)

{
	if (lock==0) PANIC();
	if (!lock->init)
	   machdep_stdinitlock(lock);
	pthread_mutex_lock(lock->mutex);
}

void machdep_stdunlock(struct stdlock* lock)

{
	if (lock==0) PANIC();
	if (!lock->init)
	   machdep_stdinitlock(lock);
	pthread_mutex_unlock(lock->mutex);
}

int machdep_stdtrylock(struct stdlock* lock)

{
	if (lock==0) PANIC();
	if (!lock->init)
	   machdep_stdinitlock(lock);
	return pthread_mutex_trylock(lock->mutex);
}

int machdep_stdtryunlock(struct stdlock* lock)

{
	if (lock==0) PANIC();
	if (!lock->init)
	   machdep_stdinitlock(lock);
	if (pthread_mutex_trylock(lock->mutex))
	   return pthread_mutex_unlock(lock->mutex);
	return 0;
}

extern void (*_libc_stdlock)(struct stdlock* lock);
extern void (*_libc_stdunlock)(struct stdlock* lock);
extern int (*_libc_stdtrylock)(struct stdlock* lock);
extern int (*_libc_stdtryunlock)(struct stdlock* lock);

int machdep_sys_init()

{
	typedef void (*voidfunc_t)();
	extern voidfunc_t _libc_read;
	extern voidfunc_t _libc_write;
	extern voidfunc_t _libc_readv;
	extern voidfunc_t _libc_writev;
	extern voidfunc_t _libc_open;
	extern voidfunc_t _libc_close;
	extern voidfunc_t _libc_fork;
	extern voidfunc_t _libc_fcntl;
	extern voidfunc_t _libc_dup;
	extern voidfunc_t _libc_pipe;
	extern voidfunc_t _libc_select;
	extern voidfunc_t _libc_malloc;
	extern voidfunc_t _libc_realloc;
	extern voidfunc_t _libc_free;
	extern ssize_t pthread_read (int , char*, int );
	extern ssize_t pthread_write (int , char*, int );
	extern int pthread_close (int);
	extern int pthread_dup (int);
	extern int pthread_fork ();
	extern int pthread_pipe (int*);
	extern int pthread_fcntl(int, int, ...);
	extern int pthread_open(const char *, int, ...);
	extern ssize_t pthread_readv (int , const struct iovec *, int );
	extern ssize_t pthread_writev (int , const struct iovec *, int );
	extern int pthread_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
	extern int pthread_getmsg(int , struct strbuf *, struct strbuf *,int*);
	extern int pthread_putmsg(int , const struct strbuf *, const struct strbuf *,int);
	extern void (*pthread_signal(int , void (*)(int)))(int);
	extern int pthread_sigaction(int,const struct sigaction *, struct sigaction *);
	extern int pthread_sigprocmask(int, const sigset_t *, sigset_t *);
	extern int pthread_sigsuspend(const sigset_t *);


	static struct {
		voidfunc_t *p;
		voidfunc_t f;
	} maptable[] = {
		{(voidfunc_t*)&_libc_read, (voidfunc_t) pthread_read},
		{(voidfunc_t*)&_libc_write, (voidfunc_t) pthread_write},
		{(voidfunc_t*)&_libc_readv, (voidfunc_t) pthread_readv},
		{(voidfunc_t*)&_libc_writev, (voidfunc_t) pthread_writev},
		{(voidfunc_t*)&_libc_open, (voidfunc_t) pthread_open},
		{(voidfunc_t*)&_libc_close, (voidfunc_t) pthread_close},
		{(voidfunc_t*)&_libc_fork, (voidfunc_t) pthread_fork},
		{(voidfunc_t*)&_libc_fcntl, (voidfunc_t) pthread_fcntl},
		{(voidfunc_t*)&_libc_dup, (voidfunc_t) pthread_dup},
		{(voidfunc_t*)&_libc_pipe, (voidfunc_t) pthread_pipe},
		{(voidfunc_t*)&_libc_select, (voidfunc_t) pthread_select},
		{(voidfunc_t*)&_libc_getmsg, (voidfunc_t) pthread_getmsg},
		{(voidfunc_t*)&_libc_putmsg, (voidfunc_t) pthread_putmsg},
		{(voidfunc_t*)&_libc_signal, (voidfunc_t) pthread_signal},
		{(voidfunc_t*)&_libc_sigaction, (voidfunc_t) pthread_sigaction},
		{(voidfunc_t*)&_libc_sigprocmask, (voidfunc_t) pthread_sigprocmask},
		{(voidfunc_t*)&_libc_sigsuspend, (voidfunc_t) pthread_sigsuspend},
		{(voidfunc_t*) 0, (voidfunc_t) 0}
	}; 
	register int i;

	for (i=0; maptable[i].p; i++)
	    *maptable[i].p = maptable[i].f;

        _libc_stdlock = machdep_stdlock;
        _libc_stdunlock = machdep_stdunlock;
        _libc_stdtrylock = machdep_stdtrylock;
        _libc_stdtryunlock = machdep_stdtryunlock;
	return 0;
}

#if 0
extern end;
char* nd = (char*) &end;
char* brk(const char* endds)

{
	register int ret;

	if ((ret = machdep_sys_brk((char*)endds))<0) {
	   SET_ERRNO(-ret);
	   return (char*) -1;
	}
	else {
	   nd = (char*) endds;
	   return 0;
	}
}

char *sbrk(int incr)

{
	register char* ret;
	if (incr!=0 && (ret=brk(nd + incr))!=0)
	   return ret;
	else
	   return nd - incr;
}
#endif

sigset_t sigmask(int sig)

{
    sigset_t oset;
    sigemptyset(&oset);
    sigaddset(&oset, sig);
    return oset;
}

sigset_t sigsetmask(sigset_t set)

{
     sigset_t oset;
     sigprocmask(SIG_SETMASK,&set,&oset);
     return oset;
}

sigset_t sigblock(sigset_t set)

{
     sigset_t oset;
     sigprocmask(SIG_BLOCK,&set,&oset);
     return oset;
}