summaryrefslogtreecommitdiffstats
path: root/qemu/target-moxie/helper.c
blob: d51e9b9cc4d63bce3b0ee031758d1c92514d179e (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
/*
 *  Moxie helper routines.
 *
 *  Copyright (c) 2008, 2009, 2010, 2013 Anthony Green
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"

#include "cpu.h"
#include "mmu.h"
#include "exec/exec-all.h"
#include "exec/cpu_ldst.h"
#include "qemu/host-utils.h"
#include "exec/helper-proto.h"

/* Try to fill the TLB and return an exception if error. If retaddr is
   NULL, it means that the function was called in C code (i.e. not
   from generated code or from helper.c) */
void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
              uintptr_t retaddr)
{
    int ret;

    ret = moxie_cpu_handle_mmu_fault(cs, addr, is_write, mmu_idx);
    if (unlikely(ret)) {
        if (retaddr) {
            cpu_restore_state(cs, retaddr);
        }
    }
    cpu_loop_exit(cs);
}

void helper_raise_exception(CPUMoxieState *env, int ex)
{
    CPUState *cs = CPU(moxie_env_get_cpu(env));

    cs->exception_index = ex;
    /* Stash the exception type.  */
    env->sregs[2] = ex;
    /* Stash the address where the exception occurred.  */
    cpu_restore_state(cs, GETPC());
    env->sregs[5] = env->pc;
    /* Jump to the exception handline routine.  */
    env->pc = env->sregs[1];
    cpu_loop_exit(cs);
}

uint32_t helper_div(CPUMoxieState *env, uint32_t a, uint32_t b)
{
    if (unlikely(b == 0)) {
        helper_raise_exception(env, MOXIE_EX_DIV0);
        return 0;
    }
    if (unlikely(a == INT_MIN && b == -1)) {
        return INT_MIN;
    }

    return (int32_t)a / (int32_t)b;
}

uint32_t helper_udiv(CPUMoxieState *env, uint32_t a, uint32_t b)
{
    if (unlikely(b == 0)) {
        helper_raise_exception(env, MOXIE_EX_DIV0);
        return 0;
    }
    return a / b;
}

void helper_debug(CPUMoxieState *env)
{
    CPUState *cs = CPU(moxie_env_get_cpu(env));

    cs->exception_index = EXCP_DEBUG;
    cpu_loop_exit(cs);
}

#if defined(CONFIG_USER_ONLY)

void moxie_cpu_do_interrupt(CPUState *cs)
{
    CPUState *cs = CPU(moxie_env_get_cpu(env));

    cs->exception_index = -1;
}

int moxie_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
                               int rw, int mmu_idx)
{
    MoxieCPU *cpu = MOXIE_CPU(cs);

    cs->exception_index = 0xaa;
    cpu->env.debug1 = address;
    cpu_dump_state(cs, stderr, fprintf, 0);
    return 1;
}

#else /* !CONFIG_USER_ONLY */

int moxie_cpu_handle_mmu_fault(CPUState *cs, vaddr address,
                               int rw, int mmu_idx)
{
    MoxieCPU *cpu = MOXIE_CPU(cs);
    CPUMoxieState *env = &cpu->env;
    MoxieMMUResult res;
    int prot, miss;
    target_ulong phy;
    int r = 1;

    address &= TARGET_PAGE_MASK;
    prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
    miss = moxie_mmu_translate(&res, env, address, rw, mmu_idx);
    if (miss) {
        /* handle the miss.  */
        phy = 0;
        cs->exception_index = MOXIE_EX_MMU_MISS;
    } else {
        phy = res.phy;
        r = 0;
    }
    tlb_set_page(cs, address, phy, prot, mmu_idx, TARGET_PAGE_SIZE);
    return r;
}


void moxie_cpu_do_interrupt(CPUState *cs)
{
    switch (cs->exception_index) {
    case MOXIE_EX_BREAK:
        break;
    default:
        break;
    }
}

hwaddr moxie_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
{
    MoxieCPU *cpu = MOXIE_CPU(cs);
    uint32_t phy = addr;
    MoxieMMUResult res;
    int miss;

    miss = moxie_mmu_translate(&res, &cpu->env, addr, 0, 0);
    if (!miss) {
        phy = res.phy;
    }
    return phy;
}
#endif
0; } to = (uint8_t *)&acd->data; avail = *slen - sizeof(ASCIIConsoleData); get_console_data(event, to, &src_len, avail); acd->ebh.length = cpu_to_be16(sizeof(ASCIIConsoleData) + src_len); acd->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA; acd->ebh.flags |= SCLP_EVENT_BUFFER_ACCEPTED; *slen = avail - src_len; return 1; } /* triggered by SCLP's write_event_data * - write console data to character layer * returns < 0 if an error occurred */ static ssize_t write_console_data(SCLPEvent *event, const uint8_t *buf, size_t len) { SCLPConsole *scon = DO_UPCAST(SCLPConsole, event, event); if (!scon->chr) { /* If there's no backend, we can just say we consumed all data. */ return len; } return qemu_chr_fe_write_all(scon->chr, buf, len); } static int write_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr) { int rc; int length; ssize_t written; ASCIIConsoleData *acd = (ASCIIConsoleData *) evt_buf_hdr; length = be16_to_cpu(evt_buf_hdr->length) - sizeof(EventBufferHeader); written = write_console_data(event, (uint8_t *)acd->data, length); rc = SCLP_RC_NORMAL_COMPLETION; /* set event buffer accepted flag */ evt_buf_hdr->flags |= SCLP_EVENT_BUFFER_ACCEPTED; /* written will be zero if a pty is not connected - don't treat as error */ if (written < 0) { /* event buffer not accepted due to error in character layer */ evt_buf_hdr->flags &= ~(SCLP_EVENT_BUFFER_ACCEPTED); rc = SCLP_RC_CONTAINED_EQUIPMENT_CHECK; } return rc; } static const VMStateDescription vmstate_sclpconsole = { .name = "sclpconsole", .version_id = 0, .minimum_version_id = 0, .fields = (VMStateField[]) { VMSTATE_BOOL(event.event_pending, SCLPConsole), VMSTATE_UINT8_ARRAY(iov, SCLPConsole, SIZE_BUFFER_VT220), VMSTATE_UINT32(iov_sclp, SCLPConsole), VMSTATE_UINT32(iov_bs, SCLPConsole), VMSTATE_UINT32(iov_data_len, SCLPConsole), VMSTATE_UINT32(iov_sclp_rest, SCLPConsole), VMSTATE_END_OF_LIST() } }; /* qemu object creation and initialization functions */ /* tell character layer our call-back functions */ static int console_init(SCLPEvent *event) { static bool console_available; SCLPConsole *scon = DO_UPCAST(SCLPConsole, event, event); if (console_available) { error_report("Multiple VT220 operator consoles are not supported"); return -1; } console_available = true; if (scon->chr) { qemu_chr_add_handlers(scon->chr, chr_can_read, chr_read, NULL, scon); } return 0; } static void console_reset(DeviceState *dev) { SCLPEvent *event = SCLP_EVENT(dev); SCLPConsole *scon = DO_UPCAST(SCLPConsole, event, event); event->event_pending = false; scon->iov_sclp = 0; scon->iov_bs = 0; scon->iov_data_len = 0; scon->iov_sclp_rest = 0; scon->notify = false; } static int console_exit(SCLPEvent *event) { return 0; } static Property console_properties[] = { DEFINE_PROP_CHR("chardev", SCLPConsole, chr), DEFINE_PROP_END_OF_LIST(), }; static void console_class_init(ObjectClass *klass, void *data) { DeviceClass *dc = DEVICE_CLASS(klass); SCLPEventClass *ec = SCLP_EVENT_CLASS(klass); dc->props = console_properties; dc->reset = console_reset; dc->vmsd = &vmstate_sclpconsole; ec->init = console_init; ec->exit = console_exit; ec->get_send_mask = send_mask; ec->get_receive_mask = receive_mask; ec->can_handle_event = can_handle_event; ec->read_event_data = read_event_data; ec->write_event_data = write_event_data; set_bit(DEVICE_CATEGORY_INPUT, dc->categories); } static const TypeInfo sclp_console_info = { .name = "sclpconsole", .parent = TYPE_SCLP_EVENT, .instance_size = sizeof(SCLPConsole), .class_init = console_class_init, .class_size = sizeof(SCLPEventClass), }; static void register_types(void) { type_register_static(&sclp_console_info); } type_init(register_types)