summaryrefslogtreecommitdiff
path: root/mobicore/MobiCoreDriverLib/Kernel/Platforms/Generic/CMcKMod.cpp
blob: 392b193ab09c8fb5bd178a2e86b32a9070d42fb5 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * Copyright (c) 2013-2014 TRUSTONIC LIMITED
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * <t-base Driver Kernel Module Interface.
 */
#include <cstdlib>

#include <sys/mman.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <inttypes.h>
#include <cstring>

#include "McTypes.h"
#include "mc_linux.h"
#include "mcVersionHelper.h"

#include "CMcKMod.h"

#include "log.h"

//------------------------------------------------------------------------------
MC_CHECK_VERSION(MCDRVMODULEAPI, 1, 1);

//------------------------------------------------------------------------------
mcResult_t CMcKMod::mapWsm(
    uint32_t    len,
    uint32_t    *pHandle,
    addr_t      *pVirtAddr)
{
    int ret = 0;
    LOG_V(" mapWsm(): len=%d", len);

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    // mapping response data is in the buffer
    struct mc_ioctl_map mapParams;

    mapParams.len = len;
    ret = ioctl(fdKMod, MC_IO_MAP_WSM, &mapParams);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_MAP_WSM");
        return MAKE_MC_DRV_KMOD_WITH_ERRNO(errno);
    }

    addr_t virtAddr = ::mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED,
                             fdKMod, mapParams.phys_addr);
    if (virtAddr == MAP_FAILED) {
        LOG_ERRNO("mmap");
        (void)ioctl(fdKMod, MC_IO_FREE, mapParams.handle);
        return MAKE_MC_DRV_KMOD_WITH_ERRNO(errno);
    }


    LOG_V(" mapped to %p, handle=%d", virtAddr,
          mapParams.handle);

    if (pVirtAddr != NULL) {
        *pVirtAddr = virtAddr;
    }

    if (pHandle != NULL) {
        *pHandle = mapParams.handle;
    }

    return 0;
}

//------------------------------------------------------------------------------
mcResult_t CMcKMod::mapMCI(
    uint32_t    len,
    addr_t      *pVirtAddr,
    bool        *pReuse)
{
    LOG_I("Mapping MCI: len=%d", len);
    // mapping response data is in the buffer
    struct mc_ioctl_map mapParams;

    mapParams.len = len;
    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    int ret = ioctl(fdKMod, MC_IO_MAP_MCI, &mapParams);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_MAP_MCI");
        return MAKE_MC_DRV_KMOD_WITH_ERRNO(errno);
    }

    // MCI is defined as offset 0, so we do not pass the mapParams.phys_addr to mmap.
    addr_t virtAddr = ::mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED,
                             fdKMod, 0);
    if (virtAddr == MAP_FAILED) {
        LOG_ERRNO("mmap");
        return MAKE_MC_DRV_KMOD_WITH_ERRNO(errno);
    }

    *pReuse = mapParams.reused;

    LOG_V(" MCI mapped to %p, handle=%d, reused=%s",
    	  (void *)virtAddr, mapParams.handle,
    	  mapParams.reused ? "true" : "false");

    if (pVirtAddr != NULL) {
        *pVirtAddr = (void *)virtAddr;
    }

    return MC_DRV_OK;
}

//------------------------------------------------------------------------------
int CMcKMod::read(addr_t buffer, uint32_t len)
{
    int ret = 0;

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    ret = ::read(fdKMod, buffer, len);
    if (ret == -1) {
        LOG_ERRNO("read");
    }
    return ret;
}


//------------------------------------------------------------------------------
bool CMcKMod::waitSSIQ(uint32_t *pCnt)
{
    uint32_t cnt;
    if (read(&cnt, sizeof(cnt)) != sizeof(cnt)) {
        return false;
    }

    if (pCnt != NULL) {
        *pCnt = cnt;
    }

    return true;
}


//------------------------------------------------------------------------------
int CMcKMod::fcInit(uint32_t nqLength, uint32_t mcpOffset, uint32_t mcpLength)
{
    int ret = 0;

    if (!isOpen()) {
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    // Init MC with NQ and MCP buffer addresses
    struct mc_ioctl_init fcInitParams = {
nq_length :
        nqLength,
mcp_offset :
        mcpOffset,
mcp_length :
        mcpLength
    };
    ret = ioctl(fdKMod, MC_IO_INIT, &fcInitParams);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_INIT");
    }

    return ret;
}

//------------------------------------------------------------------------------
int CMcKMod::fcInfo(uint32_t extInfoId, uint32_t *pState, uint32_t *pExtInfo)
{
    int ret = 0;

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    // Init MC with NQ and MCP buffer addresses
    struct mc_ioctl_info fcInfoParams;

    fcInfoParams.ext_info_id = extInfoId;
    ret = ioctl(fdKMod, MC_IO_INFO, &fcInfoParams);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_INFO");
        return ret;
    }

    if (pState != NULL) {
        *pState = fcInfoParams.state;
    }

    if (pExtInfo != NULL) {
        *pExtInfo = fcInfoParams.ext_info;
    }

    return ret;
}


//------------------------------------------------------------------------------
int CMcKMod::fcYield(void)
{
    int ret = 0;

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    ret = ioctl(fdKMod, MC_IO_YIELD, NULL);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_YIELD");
        LOG_E("ret = %d", ret);
    }

    return ret;
}


//------------------------------------------------------------------------------

int CMcKMod::fcNSIQ(void)
{
    int ret = 0;

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return  MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    ret = ioctl(fdKMod, MC_IO_NSIQ, NULL);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_NSIQ");
        LOG_E("ret = %d", ret);
    }

    return ret;
}


//------------------------------------------------------------------------------
mcResult_t CMcKMod::free(uint32_t handle, addr_t buffer, uint32_t len)
{
    LOG_V("free(): handle=%d", handle);

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    // Even if unmap fails we still go on with our request
    if (::munmap(buffer, len)) {
        LOG_I("buffer = %p, len = %d", buffer, len);
        LOG_ERRNO("munmap failed");
    }

    int ret = ioctl(fdKMod, MC_IO_FREE, handle);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_FREE");
        return MAKE_MC_DRV_KMOD_WITH_ERRNO(errno);
    }

    return MC_DRV_OK;
}


//------------------------------------------------------------------------------
mcResult_t CMcKMod::registerWsmL2(
    addr_t      buffer,
    uint32_t    len,
    uint32_t    pid,
    uint32_t    *pHandle,
    uint64_t      *pPhysWsmL2)
{
    LOG_I(" Registering virtual buffer at %p, len=%d as World Shared Memory", buffer, len);

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    struct mc_ioctl_reg_wsm params;

    params.buffer = (uintptr_t)buffer;
    params.len = len;
    params.pid = pid;
    int ret = ioctl(fdKMod, MC_IO_REG_WSM, &params);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_REG_WSM");
        return MAKE_MC_DRV_KMOD_WITH_ERRNO(errno);
    }

    LOG_I(" Registered, handle=%d, L2 phys=0x%llx ", params.handle, params.table_phys);

    if (pHandle != NULL) {
        *pHandle = params.handle;
    }

    if (pPhysWsmL2 != NULL) {
        *pPhysWsmL2 = params.table_phys;
    }

    return MC_DRV_OK;
}


//------------------------------------------------------------------------------
mcResult_t CMcKMod::unregisterWsmL2(uint32_t handle)
{
    LOG_I(" Unregistering World Shared Memory with handle %d", handle);

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    int ret = ioctl(fdKMod, MC_IO_UNREG_WSM, handle);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_UNREG_WSM");
        return MAKE_MC_DRV_KMOD_WITH_ERRNO(errno);
    }

    return MC_DRV_OK;
}

//------------------------------------------------------------------------------
mcResult_t CMcKMod::lockWsmL2(uint32_t handle)
{
    int ret = 0;

    LOG_I(" Locking World Shared Memory with handle %d", handle);

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    ret = ioctl(fdKMod, MC_IO_LOCK_WSM, handle);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_LOCK_WSM");
        LOG_E("ret = %d", ret);
    }

    return ret;
}

//------------------------------------------------------------------------------
mcResult_t CMcKMod::unlockWsmL2(uint32_t handle)
{
    int ret = 0;

    LOG_I(" Unlocking World Shared Memory with handle %d", handle);

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    ret = ioctl(fdKMod, MC_IO_UNLOCK_WSM, handle);
    // Failure here is not really important
    if (ret != 0) {
        LOG_I("ret = %d", ret);
    }

    return ret;
}


//------------------------------------------------------------------------------
uint64_t CMcKMod::findWsmL2(uint32_t handle, int fd)
{
    int ret = 0;

    struct mc_ioctl_resolv_wsm wsm;

    wsm.handle = handle;
    wsm.fd = fd;
    wsm.phys = 0;

    LOG_I(" Resolving the WSM l2 for handle=%u", handle);

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return 0;
    }

    ret = ioctl(fdKMod, MC_IO_RESOLVE_WSM, &wsm);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_RESOLVE_WSM");
        LOG_E("ret = %d", ret);
        return 0;
    }

    return wsm.phys;
}

//------------------------------------------------------------------------------
mcResult_t CMcKMod::findContiguousWsm(uint32_t handle, int fd, uint64_t *phys, uint32_t *len)
{
    mcResult_t ret = MC_DRV_OK;
    struct mc_ioctl_resolv_cont_wsm wsm;

    wsm.handle = handle;
    wsm.phys = 0;
    wsm.length = 0;
    wsm.fd = fd;

    LOG_I(" Resolving the contiguous WSM l2 for handle=%u", handle);

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    ret = ioctl(fdKMod, MC_IO_RESOLVE_CONT_WSM, &wsm);
    if (ret != 0) {
        LOG_W("ioctl MC_IO_RESOLVE_CONT_WSM failed with \"%s\"(errno %i)", strerror(errno), errno);
    } else {
        *phys = wsm.phys;
        *len = wsm.length;
    }

    return ret;
}

//------------------------------------------------------------------------------
mcResult_t CMcKMod::cleanupWsmL2(void)
{
    int ret = 0;

    LOG_I(" Cleaning up the orphaned bulk buffers");

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    ret = ioctl(fdKMod, MC_IO_CLEAN_WSM, 0);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_CLEAN_WSM");
        LOG_E("ret = %d", ret);
    }

    return ret;
}

//------------------------------------------------------------------------------
mcResult_t CMcKMod::setupLog(void)
{
    int ret = 0;

    LOG_I(" Setting up the memory logging system");

    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return MC_DRV_ERR_KMOD_NOT_OPEN;
    }

    ret = ioctl(fdKMod, MC_IO_LOG_SETUP, 0);
    if (ret != 0) {
        LOG_W("ioctl MC_IO_LOG_SETUP failed with \"%s\"(errno %i)", strerror(errno), errno);
    }

    return ret;
}

//------------------------------------------------------------------------------
bool CMcKMod::checkVersion(void)
{
    uint32_t version;
    if (!isOpen()) {
        LOG_E("no connection to kmod");
        return false;
    }

    int ret = ioctl(fdKMod, MC_IO_VERSION, &version);
    if (ret != 0) {
        LOG_ERRNO("ioctl MC_IO_VERSION");
        LOG_E("ret = %d", ret);
        return false;
    }

    // Run-time check.
    char *errmsg;
    if (!checkVersionOkMCDRVMODULEAPI(version, &errmsg)) {
        LOG_E("%s", errmsg);
        return false;
    }
    LOG_I("%s", errmsg);

    return true;
}