summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/hppa1.1/simhppa/tty/tty.c
blob: b89a707739ff57edea967621c173589200a38f25 (plain) (blame)
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
/*
 *  Tty IO Driver
 *  This is a "libio" driver based on libc/support/generic/libio interface
 *  which is on top of the RTEMS IO manager.
 *
 *  These provide UNIX-like read and write calls for the C library.
 *
 *  COPYRIGHT (c) 1994 by Division Incorporated
 *
 *  The license and distribution terms for this file may in
 *  the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id$
 */

#include <bsp.h>
#include <rtems/libio.h>

#include <errno.h>

#define PRINT_BUFFER_SIZE    (16 * 1024)

/*
 * NOTE: this structure is dumplicated in print_dump.c utility
 */

struct {
    int  index;
    int  size;
    char buffer[PRINT_BUFFER_SIZE];
} print_buffer;

/* always use printf buffer if non-zero */
int  use_print_buffer;

static int host_read_syscall(int fd, char *buffer, int count);
static int host_write_syscall(int fd, char *buffer, int count);

rtems_device_driver
tty_initialize(
    rtems_device_major_number major,
    rtems_device_minor_number minor,
    void                    * arg
  )
{
    rtems_status_code status;
    
    status = rtems_io_register_name("/dev/tty00",
                                    major,
                                    (rtems_device_minor_number) 0);
    if (status != RTEMS_SUCCESSFUL)
        rtems_fatal_error_occurred(status);

    return RTEMS_SUCCESSFUL;
}

rtems_device_driver
tty_open(
    rtems_device_major_number major,
    rtems_device_minor_number minor,
    void                    * arg
  )
{
    return RTEMS_SUCCESSFUL;
}

rtems_device_driver
tty_close(
    rtems_device_major_number major,
    rtems_device_minor_number minor,
    void                    * arg
  )
{
    return RTEMS_SUCCESSFUL;
}

rtems_device_driver
tty_control(
    rtems_device_major_number major,
    rtems_device_minor_number minor,
    void                    * arg
  )
{
    return RTEMS_SUCCESSFUL;
}


rtems_device_driver
tty_read(
    rtems_device_major_number major,
    rtems_device_minor_number minor,
    void                    * arg
  )
{
    rtems_libio_rw_args_t *rw_args;
    int count = 0;

    rw_args = (rtems_libio_rw_args_t *) arg;

    /*
     * If we are printing to a buffer, then just return newline on all
     * read's.  If we return 0 bytes read, then the pause() calls in
     * the RTEMS tests get hosed (pause() does a gets())
     */

    if ( use_print_buffer )
    {
        *rw_args->buffer = '\n';
        count = 1;
    }
    else
    {
        count = host_read_syscall(0, rw_args->buffer, rw_args->count);
    }

    if (count >= 0)
    {
        rw_args->bytes_moved = count;
        return RTEMS_SUCCESSFUL;
    }
    return RTEMS_UNSATISFIED;
}

rtems_device_driver
tty_write(
    rtems_device_major_number major,
    rtems_device_minor_number minor,
    void                    * arg
  )
{
    unsigned32 level;
    rtems_libio_rw_args_t *rw_args;
    int count = 0;
    int fd = 1;                 /* XXX fixme; needs to be saved in iop */

    rw_args = (rtems_libio_rw_args_t *) arg;

    /*
     * HACK alert
     *
     * Some of the simulators have real problems when multi cpu and
     * using the system calls.  Until this is fixed, if we are multi
     * cpu then we write to a printf buffer
     */

    if ( use_print_buffer )
    {
        /* save size in memory for dumper */
        if (print_buffer.size == 0)
            print_buffer.size = PRINT_BUFFER_SIZE;

        while (rw_args->count-- > 0)
        {
            rtems_interrupt_disable(level);
            print_buffer.buffer[print_buffer.index] = *rw_args->buffer++;
            print_buffer.index++;
            print_buffer.index &= (PRINT_BUFFER_SIZE - 1);
            print_buffer.buffer[print_buffer.index] = 0;
            rtems_interrupt_enable(level);
            count++;
        }
    }
    else
    {
#if 1
        /*
         * if on a multi cpu system and writing to stdout, redirect to stderr
         * so we can keep them separate
         */

        if ((cpu_number == 1) && (fd == 1))
            fd = 2;
#endif
        count = host_write_syscall(fd, rw_args->buffer, rw_args->count);
    }

    if (count >= 0)
    {
        rw_args->bytes_moved = count;
        return RTEMS_SUCCESSFUL;
    }
    return RTEMS_UNSATISFIED;
}


/*
 * Host system call hack.
 * This little trick gets all the args in the right registers
 * for the system call and permits simpler inline asm.
 * Since this whole thing (syscalls under simulator) is a hack,
 * this little bit more is not going to hurt anything.
 */


static int
host_read_syscall(
    int fd,
    char *buffer,
    int count
  )
{                   
    unsigned32 level;
    int rc;

    rtems_interrupt_disable(level);

    /* This is an HPUX system call, with return value copied out */
    asm volatile (" stw     %%r19,-28(0,%%r30)\n\
                    ldil    L%%0xc0000000,%%r1\n\
                    ble     4(7,%%r1)\n\
                    ldi     3,%%r22\n\
                    ldw     -28(0,%%r30),%%r19\n\
                    copy    %%r28, %0"
                  : "=r" (rc)
                  : );

    rtems_interrupt_enable(level);
    return rc;
}

static int
host_write_syscall(
    int fd,
    char *buffer,
    int count
  )
{                   
    unsigned32 level;
    int rc;

    rtems_interrupt_disable(level);

    /* This is an HPUX system call, with return value copied out */
    asm volatile (" stw     %%r19,-28(0,%%r30)\n\
                    ldil    L%%0xc0000000,%%r1\n\
                    ble     4(7,%%r1)\n\
                    ldi     4,%%r22\n\
                    ldw     -28(0,%%r30),%%r19\n\
                    copy    %%r28, %0"
                  : "=r" (rc)
                  : );

    rtems_interrupt_enable(level);
    return rc;
}