aboutsummaryrefslogtreecommitdiff
path: root/reginfo.c
blob: 1f55d065d4af3b7f73a51738c86901b793df097c (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
/******************************************************************************
 * Copyright (c) 2017 Linaro Limited
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Peter Maydell (Linaro) - initial implementation
 *****************************************************************************/

#include <stdio.h>
#include <string.h>

#include "risu.h"

struct reginfo master_ri, apprentice_ri;

uint8_t apprentice_memblock[MEMBLOCKLEN];

static int mem_used;
static int packet_mismatch;

int send_register_info(write_fn write_fn, void *uc)
{
    struct reginfo ri;
    trace_header_t header;
    int op;

    reginfo_init(&ri, uc);
    op = get_risuop(&ri);

    /* Write a header with PC/op to keep in sync */
    header.pc = get_pc(&ri);
    header.risu_op = op;
    if (write_fn(&header, sizeof(header)) != 0) {
        return -1;
    }

    switch (op) {
    case OP_TESTEND:
        write_fn(&ri, sizeof(ri));
        /* if we are tracing write_fn will return 0 unlike a remote
           end, hence we force return of 1 here */
        return 1;
    case OP_SETMEMBLOCK:
        memblock = (void *)(uintptr_t)get_reginfo_paramreg(&ri);
        break;
    case OP_GETMEMBLOCK:
        set_ucontext_paramreg(uc,
                              get_reginfo_paramreg(&ri) + (uintptr_t)memblock);
        break;
    case OP_COMPAREMEM:
        return write_fn(memblock, MEMBLOCKLEN);
        break;
    case OP_COMPARE:
    default:
        /* Do a simple register compare on (a) explicit request
         * (b) end of test (c) a non-risuop UNDEF
         */
        return write_fn(&ri, sizeof(ri));
    }
    return 0;
}

/* Read register info from the socket and compare it with that from the
 * ucontext. Return 0 for match, 1 for end-of-test, 2 for mismatch.
 * NB: called from a signal handler.
 *
 * We don't have any kind of identifying info in the incoming data
 * that says whether it is register or memory data, so if the two
 * sides get out of sync then we will fail obscurely.
 */
int recv_and_compare_register_info(read_fn read_fn,
                                   respond_fn resp_fn, void *uc)
{
    int resp = 0, op;
    trace_header_t header;

    reginfo_init(&master_ri, uc);
    op = get_risuop(&master_ri);

    if (read_fn(&header, sizeof(header)) != 0) {
        return -1;
    }

    if (header.risu_op != op) {
        /* We are out of sync */
        resp = 2;
        resp_fn(resp);
        return resp;
    }

    /* send OK for the header */
    resp_fn(0);

    switch (op) {
    case OP_COMPARE:
    case OP_TESTEND:
    default:
        /* Do a simple register compare on (a) explicit request
         * (b) end of test (c) a non-risuop UNDEF
         */
        if (read_fn(&apprentice_ri, sizeof(apprentice_ri))) {
            packet_mismatch = 1;
            resp = 2;
        } else if (!reginfo_is_eq(&master_ri, &apprentice_ri)) {
            /* register mismatch */
            resp = 2;
        } else if (op == OP_TESTEND) {
            resp = 1;
        }
        resp_fn(resp);
        break;
    case OP_SETMEMBLOCK:
        memblock = (void *)(uintptr_t)get_reginfo_paramreg(&master_ri);
        break;
    case OP_GETMEMBLOCK:
        set_ucontext_paramreg(uc, get_reginfo_paramreg(&master_ri) +
                              (uintptr_t)memblock);
        break;
    case OP_COMPAREMEM:
        mem_used = 1;
        if (read_fn(apprentice_memblock, MEMBLOCKLEN)) {
            packet_mismatch = 1;
            resp = 2;
        } else if (memcmp(memblock, apprentice_memblock, MEMBLOCKLEN) != 0) {
            /* memory mismatch */
            resp = 2;
        }
        resp_fn(resp);
        break;
    }

    return resp;
}

/* Print a useful report on the status of the last comparison
 * done in recv_and_compare_register_info(). This is called on
 * exit, so need not restrict itself to signal-safe functions.
 * Should return 0 if it was a good match (ie end of test)
 * and 1 for a mismatch.
 */
int report_match_status(int trace)
{
    int resp = 0;
    fprintf(stderr, "match status...\n");
    if (packet_mismatch) {
        fprintf(stderr, "packet mismatch (probably disagreement "
                "about UNDEF on load/store)\n");
        /* We don't have valid reginfo from the apprentice side
         * so stop now rather than printing anything about it.
         */
        fprintf(stderr, "%s reginfo:\n", trace ? "this" : "master");
        reginfo_dump(&master_ri, stderr);
        return 1;
    }
    if (!reginfo_is_eq(&master_ri, &apprentice_ri)) {
        fprintf(stderr, "mismatch on regs!\n");
        resp = 1;
    }
    if (mem_used
        && memcmp(memblock, &apprentice_memblock, MEMBLOCKLEN) != 0) {
        fprintf(stderr, "mismatch on memory!\n");
        resp = 1;
    }
    if (!resp) {
        fprintf(stderr, "match!\n");
        return 0;
    }

    fprintf(stderr, "%s reginfo:\n", trace ? "this" : "master");
    reginfo_dump(&master_ri, stderr);
    fprintf(stderr, "%s reginfo:\n", trace ? "trace" : "apprentice");
    reginfo_dump(&apprentice_ri, stderr);

    if (trace) {
        reginfo_dump_mismatch(&apprentice_ri, &master_ri, stderr);
    } else {
        reginfo_dump_mismatch(&master_ri, &apprentice_ri, stderr);
    }
    return resp;
}