summaryrefslogtreecommitdiff
path: root/device/kmsg.c
blob: dbf6b8a0bd4b42abae8d993d3185716184de1616 (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
/* GNU Mach Kernel Message Device.

   Copyright (C) 1998, 1999, 2007 Free Software Foundation, Inc.

   Written by OKUJI Yoshinori.

This 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.

This software 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 software; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* kmsg provides a stream interface.  */

#include <sys/types.h>
#include <string.h>

#include <device/conf.h>
#include <device/ds_routines.h>
#include <device/io_req.h>
#include <mach/boolean.h>
#include <kern/lock.h>
#include <device/kmsg.h>


#define KMSGBUFSIZE	(4096)  /* XXX */

/* Simple array for buffering messages */
static char kmsg_buffer[KMSGBUFSIZE];
/* Point to the offset to write */
static int kmsg_write_offset;
/* Point to the offset to read */
static int kmsg_read_offset;
/* I/O request queue for blocking read */
static queue_head_t kmsg_read_queue;
/* Used for exclusive access to the device */
static boolean_t kmsg_in_use;
/* Used for exclusive access to the routines */
static struct lock kmsg_lock_data;
#define kmsg_lock_init()	lock_init (&kmsg_lock_data, FALSE)
#define kmsg_lock()		lock_write (&kmsg_lock_data)
#define kmsg_unlock()		lock_write_done (&kmsg_lock_data)
/* If already initialized or not  */
static boolean_t kmsg_init_done = FALSE;

/* Kernel Message Initializer */
static void
kmsginit (void)
{
  kmsg_write_offset = 0;
  kmsg_read_offset = 0;
  queue_init (&kmsg_read_queue);
  kmsg_in_use = FALSE;
  kmsg_lock_init ();
}

/* Kernel Message Open Handler */
io_return_t
kmsgopen (dev_t dev, int flag, const io_req_t ior)
{
  kmsg_lock ();
  if (kmsg_in_use)
    {
      kmsg_unlock ();
      return D_ALREADY_OPEN;
    }
  
  kmsg_in_use = TRUE;

  kmsg_unlock ();
  return D_SUCCESS;
}

/* Kernel Message Close Handler */
void
kmsgclose (dev_t dev, int flag)
{
  kmsg_lock ();
  kmsg_in_use = FALSE;

  kmsg_unlock ();
}

static boolean_t kmsg_read_done (io_req_t ior);

/* Kernel Message Read Handler */
io_return_t
kmsgread (dev_t dev, io_req_t ior)
{
  int err;
  int amt, len;
  
  err = device_read_alloc (ior, ior->io_count);
  if (err != KERN_SUCCESS)
    return err;

  kmsg_lock ();
  if (kmsg_read_offset == kmsg_write_offset)
    {
      /* The queue is empty.  */
      if (ior->io_mode & D_NOWAIT)
	{
	  kmsg_unlock ();
	  return D_WOULD_BLOCK;
	}

      ior->io_done = kmsg_read_done;
      enqueue_tail (&kmsg_read_queue, (queue_entry_t) ior);
      kmsg_unlock ();
      return D_IO_QUEUED;
    }

  len = kmsg_write_offset - kmsg_read_offset;
  if (len < 0)
    len += KMSGBUFSIZE;

  amt = ior->io_count;
  if (amt > len)
    amt = len;
  
  if (kmsg_read_offset + amt <= KMSGBUFSIZE)
    {
      memcpy (ior->io_data, kmsg_buffer + kmsg_read_offset, amt);
    }
  else
    {
      int cnt;

      cnt = KMSGBUFSIZE - kmsg_read_offset;
      memcpy (ior->io_data, kmsg_buffer + kmsg_read_offset, cnt);
      memcpy (ior->io_data + cnt, kmsg_buffer, amt - cnt);
    }

  kmsg_read_offset += amt;
  if (kmsg_read_offset >= KMSGBUFSIZE)
    kmsg_read_offset -= KMSGBUFSIZE;
  
  ior->io_residual = ior->io_count - amt;

  kmsg_unlock ();
  return D_SUCCESS;
}

static boolean_t
kmsg_read_done (io_req_t ior)
{
  int amt, len;

  kmsg_lock ();
  if (kmsg_read_offset == kmsg_write_offset)
    {
      /* The queue is empty.  */
      ior->io_done = kmsg_read_done;
      enqueue_tail (&kmsg_read_queue, (queue_entry_t) ior);
      kmsg_unlock ();
      return FALSE;
    }

  len = kmsg_write_offset - kmsg_read_offset;
  if (len < 0)
    len += KMSGBUFSIZE;

  amt = ior->io_count;
  if (amt > len)
    amt = len;
  
  if (kmsg_read_offset + amt <= KMSGBUFSIZE)
    {
      memcpy (ior->io_data, kmsg_buffer + kmsg_read_offset, amt);
    }
  else
    {
      int cnt;

      cnt = KMSGBUFSIZE - kmsg_read_offset;
      memcpy (ior->io_data, kmsg_buffer + kmsg_read_offset, cnt);
      memcpy (ior->io_data + cnt, kmsg_buffer, amt - cnt);
    }

  kmsg_read_offset += amt;
  if (kmsg_read_offset >= KMSGBUFSIZE)
    kmsg_read_offset -= KMSGBUFSIZE;
  
  ior->io_residual = ior->io_count - amt;

  kmsg_unlock ();
  ds_read_done (ior);
  
  return TRUE;
}

io_return_t
kmsggetstat (dev_t dev, int flavor, int *data, unsigned int *count)
{
  switch (flavor)
    {
    case DEV_GET_SIZE:
      data[DEV_GET_SIZE_DEVICE_SIZE] = 0;
      data[DEV_GET_SIZE_RECORD_SIZE] = 1;
      *count = DEV_GET_SIZE_COUNT;
      break;

    default:
      return D_INVALID_OPERATION;
    }

  return D_SUCCESS;
}

/* Write to Kernel Message Buffer */
void
kmsg_putchar (int c)
{
  io_req_t ior;
  int offset;

  /* XXX: cninit is not called before cnputc is used. So call kmsginit
     here if not initialized yet.  */
  if (!kmsg_init_done)
    {
      kmsginit ();
      kmsg_init_done = TRUE;
    }

  kmsg_lock ();
  offset = kmsg_write_offset + 1;
  if (offset == KMSGBUFSIZE)
    offset = 0;

  if (offset == kmsg_read_offset)
    {
      /* Discard C.  */
      kmsg_unlock ();
      return;
    }

  kmsg_buffer[kmsg_write_offset++] = c;
  if (kmsg_write_offset == KMSGBUFSIZE)
    kmsg_write_offset = 0;

  while ((ior = (io_req_t) dequeue_head (&kmsg_read_queue)) != NULL)
    iodone (ior);

  kmsg_unlock ();
}