~kris/9p

9hist

ref: 64e77d4402970c246d0847745b7fe8dfd4d8dc70 9hist/pc/clock.c -rw-r--r-- 1.9 KiB
64e77d44 — David du Colombier Plan 9 from Bell Labs 1991-08-14 35 years ago
                                                                                
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
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"io.h"
#include	"ureg.h"

/*
 *  8253 timer
 */
enum
{
	T0cntr=	0x40,		/* counter ports */
	T1cntr=	0x41,		/* ... */
	T2cntr=	0x42,		/* ... */
	Tmode=	0x43,		/* mode port */

	Load0square=	0x36,		/*  load counter 0 with 2 bytes,
					 *  output a square wave whose
					 *  period is the counter period
					 */
	Freq=		1193182,	/* Real clock frequency */
	FHZ=		1000,		/* hertz for fast clock */
};

/*
 *  delay for l milliseconds
 */
void
delay(int l)
{
	int i;

	while(--l){
		for(i=0; i < 404; i++)
			;
	}
}

void
clockinit(void)
{
	/*
	 *  set vector for clock interrupts
	 */
	setvec(Clockvec, clock);

	/*
	 *  make clock output a square wave with a 1/HZ period
	 */
	outb(Tmode, Load0square);
	outb(T0cntr, (Freq/HZ));	/* low byte */
	outb(T0cntr, (Freq/HZ)>>8);	/* high byte */
}

void
clock(Ureg *ur)
{
	Proc *p;

	m->ticks++;

	checkalarms();
	mouseclock();

	/*
	 *  process time accounting
	 */
	p = m->proc;
	if(p){
		p->pc = ur->pc;
		if (p->state==Running)
			p->time[p->insyscall]++;
	}

	if(u && p && p->state==Running){
		/*
		 *  preemption
		 */
		if(anyready()){
			if(p->hasspin)
				p->hasspin = 0;
			else
				sched();
		}
		/*
		 *  notes for processes that might be spinning
		 *  in user mode.
		 */
		if((ur->cs&0xffff) == UESEL){
			if(u->nnote)
				notify(ur);
		}
	}
}

/*
 *  a faster (1 MS) clock tick for a non-interrupting serial port or
 *  kernel profiling.  m->ticks still increments as usual.
 */
void
fclock(Ureg *ur)
{
	static ulong ticks;

	uartintr0(ur);		/* poll the serial port */
	if((ticks++ % (FHZ/HZ)) == 0)
		clock(ur);
}

void
fclockinit(void)
{
	int x;

	/*
	 *  set vector for clock interrupts
	 */
	setvec(Clockvec, fclock);

	/*
	 *  make clock output a square wave with a 1/FHZ period
	 */
	x = splhi();
	outb(Tmode, Load0square);
	outb(T0cntr, (Freq/FHZ));	/* low byte */
	outb(T0cntr, (Freq/FHZ)>>8);	/* high byte */
	splx(x);
}