aboutsummaryrefslogtreecommitdiff
path: root/secure_fw/core/ipc/tfm_svcalls.c
blob: c8acf5f189a6f184e4735ae1850fbe9d8c8ae4b4 (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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/*
 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "psa_client.h"
#include "psa_service.h"
#include "tfm_svc.h"
#include "tfm_svcalls.h"
#include "tfm_thread.h"
#include "tfm_wait.h"
#include "tfm_utils.h"
#include "tfm_internal_defines.h"
#include "tfm_message_queue.h"
#include "tfm_spm.h"
#include "secure_utilities.h"
#include "tfm_api.h"
#include "tfm_secure_api.h"

#define PSA_TIMEOUT_MASK        PSA_BLOCK

/************************* SVC handler for PSA Client APIs *******************/

uint32_t tfm_svcall_psa_framework_version(void)
{
    return PSA_FRAMEWORK_VERSION;
}

uint32_t tfm_svcall_psa_version(uint32_t *args, int32_t ns_caller)
{
    uint32_t sid;
    struct tfm_spm_service_t *service;

    TFM_ASSERT(args != NULL);
    sid = (uint32_t)args[0];
    /*
     * It should return PSA_VERSION_NONE if the RoT Service is not
     * implemented.
     */
    service = tfm_spm_get_service_by_sid(sid);
    if (!service) {
        return PSA_VERSION_NONE;
    }

    /*
     * It should return PSA_VERSION_NONE if the caller is not authorized
     * to access the RoT Service.
     */
    if (ns_caller && !service->service_db->non_secure_client) {
        return PSA_VERSION_NONE;
    }

    return service->service_db->minor_version;
}

psa_handle_t tfm_svcall_psa_connect(uint32_t *args, int32_t ns_caller)
{
    uint32_t sid;
    uint32_t minor_version;
    struct tfm_spm_service_t *service;
    struct tfm_msg_body_t *msg;

    TFM_ASSERT(args != NULL);
    sid = (uint32_t)args[0];
    minor_version = (uint32_t)args[1];

    /* It is a fatal error if the RoT Service does not exist on the platform */
    service = tfm_spm_get_service_by_sid(sid);
    if (!service) {
        tfm_panic();
    }

    /*
     * It is a fatal error if the caller is not authorized to access the RoT
     * Service.
     */
    if (ns_caller && !service->service_db->non_secure_client) {
        tfm_panic();
    }

    /*
     * It is a fatal error if the version of the RoT Service requested is not
     * supported on the platform.
     */
    if (tfm_spm_check_client_version(service, minor_version) != IPC_SUCCESS) {
        tfm_panic();
    }

    /* No input or output needed for connect message */
    msg = tfm_spm_create_msg(service, PSA_NULL_HANDLE, PSA_IPC_CONNECT,
                             ns_caller, NULL, 0, NULL, 0, NULL);
    if (!msg) {
        return PSA_NULL_HANDLE;
    }

    /*
     * Send message and wake up the SP who is waiting on message queue,
     * and scheduler triggered
     */
    tfm_spm_send_event(service, msg);

    return PSA_NULL_HANDLE;
}

psa_status_t tfm_svcall_psa_call(uint32_t *args, int32_t ns_caller)
{
    psa_handle_t handle;
    psa_invec *inptr, invecs[PSA_MAX_IOVEC];
    psa_outvec *outptr, outvecs[PSA_MAX_IOVEC];
    size_t in_num, out_num;
    struct tfm_spm_service_t *service;
    struct tfm_msg_body_t *msg;
    int i;

    TFM_ASSERT(args != NULL);
    handle = (psa_handle_t)args[0];
    if (!ns_caller) {
        inptr = (psa_invec *)args[1];
        in_num = (size_t)args[2];
        outptr = (psa_outvec *)args[3];
        /*
         * FixMe: 5th parameter is pushed at stack top before SVC; plus
         * exception stacked contents, 5th parameter is now at 8th position
         * in SVC handler. However, if thread mode applies FloatPoint, then
         * FloatPoint context is pushed into stack and then 5th parameter
         * will not be args[8].
         * Will refine it later.
         */
         out_num = (size_t)args[8];
    } else {
        /*
         * FixMe: From non-secure caller, vec and len are composed into a new
         * struct parameter. Need to extract them.
         */
        if (tfm_memory_check((void *)args[1], sizeof(uint32_t),
            ns_caller) != IPC_SUCCESS) {
            tfm_panic();
        }
        if (tfm_memory_check((void *)args[2], sizeof(uint32_t),
            ns_caller) != IPC_SUCCESS) {
            tfm_panic();
        }

        inptr = (psa_invec *)((psa_invec *)args[1])->base;
        in_num = ((psa_invec *)args[1])->len;
        outptr = (psa_outvec *)((psa_invec *)args[2])->base;
        out_num = ((psa_invec *)args[2])->len;
    }

    /* It is a fatal error if in_len + out_len > PSA_MAX_IOVEC. */
    if (in_num + out_num > PSA_MAX_IOVEC) {
        tfm_panic();
    }

    /* It is a fatal error if an invalid handle was passed. */
    service = tfm_spm_get_service_by_handle(handle);
    if (!service) {
        /* FixMe: Need to implement one mechanism to resolve this failure. */
        tfm_panic();
    }

    /* It is a fatal error if an invalid memory reference was provide. */
    if (tfm_memory_check((void *)inptr, in_num * sizeof(psa_invec),
        ns_caller) != IPC_SUCCESS) {
        tfm_panic();
    }
    if (tfm_memory_check((void *)outptr, out_num * sizeof(psa_outvec),
        ns_caller) != IPC_SUCCESS) {
        tfm_panic();
    }

    tfm_memset(invecs, 0, sizeof(invecs));
    tfm_memset(outvecs, 0, sizeof(outvecs));

    /* Copy the address out to avoid TOCTOU attacks. */
    tfm_memcpy(invecs, inptr, in_num * sizeof(psa_invec));
    tfm_memcpy(outvecs, outptr, out_num * sizeof(psa_outvec));

    /*
     * It is a fatal error if an invalid payload memory reference
     * was provided.
     */
    for (i = 0; i < in_num; i++) {
        if (tfm_memory_check((void *)invecs[i].base, invecs[i].len,
            ns_caller) != IPC_SUCCESS) {
            tfm_panic();
        }
    }
    for (i = 0; i < out_num; i++) {
        if (tfm_memory_check(outvecs[i].base, outvecs[i].len,
            ns_caller) != IPC_SUCCESS) {
            tfm_panic();
        }
    }

    /*
     * FixMe: Need to check if the message is unrecognized by the RoT
     * Service or incorrectly formatted.
     */
    msg = tfm_spm_create_msg(service, handle, PSA_IPC_CALL, ns_caller, invecs,
                             in_num, outvecs, out_num, outptr);
    if (!msg) {
        /* FixMe: Need to implement one mechanism to resolve this failure. */
        tfm_panic();
    }

    /*
     * Send message and wake up the SP who is waiting on message queue,
     * and scheduler triggered
     */
    if (tfm_spm_send_event(service, msg) != IPC_SUCCESS) {
        /* FixMe: Need to refine failure process here. */
        tfm_panic();
    }
    return PSA_SUCCESS;
}

void tfm_svcall_psa_close(uint32_t *args, int32_t ns_caller)
{
    psa_handle_t handle;
    struct tfm_spm_service_t *service;
    struct tfm_msg_body_t *msg;

    TFM_ASSERT(args != NULL);
    handle = args[0];
    /* It will have no effect if called with the NULL handle */
    if (handle == PSA_NULL_HANDLE) {
        return;
    }

    /*
     * It is a fatal error if an invalid handle was provided that is not the
     * null handle..
     */
    service = tfm_spm_get_service_by_handle(handle);
    if (!service) {
        /* FixMe: Need to implement one mechanism to resolve this failure. */
        tfm_panic();
    }

    /* No input or output needed for close message */
    msg = tfm_spm_create_msg(service, handle, PSA_IPC_DISCONNECT, ns_caller,
                             NULL, 0, NULL, 0, NULL);
    if (!msg) {
        /* FixMe: Need to implement one mechanism to resolve this failure. */
        return;
    }

    /*
     * Send message and wake up the SP who is waiting on message queue,
     * and scheduler triggered
     */
    tfm_spm_send_event(service, msg);

    /* Service handle is not used anymore */
    tfm_spm_free_conn_handle(service, handle);
}

/*********************** SVC handler for PSA Service APIs ********************/

/**
 * \brief SVC handler for \ref psa_wait.
 *
 * \param[in] args              Include all input arguments:
 *                              signal_mask, timeout.
 *
 * \retval >0                   At least one signal is asserted.
 * \retval 0                    No signals are asserted. This is only seen when
 *                              a polling timeout is used.
 */
static psa_signal_t tfm_svcall_psa_wait(uint32_t *args)
{
    psa_signal_t signal_mask;
    uint32_t timeout;
    struct tfm_spm_ipc_partition_t *partition = NULL;

    TFM_ASSERT(args != NULL);
    signal_mask = (psa_signal_t)args[0];
    timeout = args[1];

    /*
     * Timeout[30:0] are reserved for future use.
     * SPM must ignore the value of RES.
     */
    timeout &= PSA_TIMEOUT_MASK;

    partition = tfm_spm_get_running_partition();
    if (!partition) {
        tfm_panic();
    }

    /*
     * Expected signals are included in signal wait mask, ignored signals
     * should not be set and affect caller thread status. Save this mask for
     * further checking while signals are ready to be set.
     */
    partition->signal_mask = signal_mask;

    /*
     * tfm_event_wait() blocks the caller thread if no signals are available.
     * In this case, the return value of this function is temporary set into
     * runtime context. After new signal(s) are available, the return value
     * is updated with the available signal(s) and blocked thread gets to run.
     */
    if ((timeout == PSA_BLOCK) && ((partition->signals & signal_mask) == 0)) {
            tfm_event_wait(&partition->signal_event);
    }

    return partition->signals & signal_mask;
}

/**
 * \brief SVC handler for \ref psa_get.
 *
 * \param[in] args              Include all input arguments: signal, msg.
 *
 * \retval PSA_SUCCESS          Success, *msg will contain the delivered
 *                              message.
 * \retval PSA_ERR_NOMSG        Message could not be delivered.
 * \retval "Does not return"    The call is invalid because one or more of the
 *                              following are true:
 * \arg                           signal has more than a single bit set.
 * \arg                           signal does not correspond to a RoT Service.
 * \arg                           The RoT Service signal is not currently
 *                                asserted.
 * \arg                           The msg pointer provided is not a valid memory
 *                                reference.
 */
static psa_status_t tfm_svcall_psa_get(uint32_t *args)
{
    psa_signal_t signal;
    psa_msg_t *msg = NULL;
    struct tfm_spm_service_t *service = NULL;
    struct tfm_msg_body_t *tmp_msg = NULL;
    struct tfm_spm_ipc_partition_t *partition = NULL;

    TFM_ASSERT(args != NULL);
    signal = (psa_signal_t)args[0];
    msg = (psa_msg_t *)args[1];

    /*
     * Only one message could be retrieved every time for psa_get(). It is a
     * fatal error if the input signal has more than a signal bit set.
     */
    if (tfm_bitcount(signal) != 1) {
        tfm_panic();
    }

    /*
     * It is a fatal error if the input msg pointer is not a valid memory
     * reference.
     */
    if (tfm_memory_check((void *)msg, sizeof(psa_msg_t),
        false) != IPC_SUCCESS) {
        tfm_panic();
    }

    partition = tfm_spm_get_running_partition();
    if (!partition) {
        tfm_panic();
    }

    /*
     * It is a fatal error if the caller call psa_get() when no message has
     * been set. The caller must call this function after a RoT Service signal
     * is returned by psa_wait().
     */
    if (partition->signals == 0) {
        tfm_panic();
    }

    /*
     * It is a fatal error if the RoT Service signal is not currently asserted.
     */
    if ((partition->signals & signal) == 0) {
        tfm_panic();
    }

    /*
     * Get Rot service by signal from partition. It is a fatal error if geting
     * failed which mean the input signal is not correspond to a RoT service.
     */
    service = tfm_spm_get_service_by_signal(partition, signal);
    if (!service) {
        tfm_panic();
    }

    tmp_msg = tfm_msg_dequeue(&service->msg_queue);
    if (!tmp_msg) {
        return PSA_ERR_NOMSG;
    }

    tfm_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t));

    /*
     * There may be mutiple messages for this RoT Service signal, do not clear
     * its mask until no remaining message.
     */
    if (tfm_msg_queue_is_empty(&service->msg_queue)) {
        partition->signals &= ~signal;
    }

    return PSA_SUCCESS;
}

/**
 * \brief SVC handler for \ref psa_set_rhandle.
 *
 * \param[in] args              Include all input arguments:
 *                              msg_handle, rhandle.
 *
 * \retval void                 Success, rhandle will be provided with all
 *                              subsequent messages delivered on this
 *                              connection.
 * \retval "Does not return"    msg_handle is invalid.
 */
static void tfm_svcall_psa_set_rhandle(uint32_t *args)
{
    psa_handle_t msg_handle;
    void *rhandle = NULL;
    struct tfm_msg_body_t *msg = NULL;

    TFM_ASSERT(args != NULL);
    msg_handle = (psa_handle_t)args[0];
    rhandle = (void *)args[1];

    /* It is a fatal error if message handle is invalid */
    msg = tfm_spm_get_msg_from_handle(msg_handle);
    if (!msg) {
        tfm_panic();
    }

    if (tfm_spm_set_rhandle(msg->service, msg->handle,
        rhandle) != IPC_SUCCESS) {
        tfm_panic();
    }
}

/**
 * \brief SVC handler for \ref psa_read.
 *
 * \param[in] args              Include all input arguments:
 *                              msg_handle, invec_idx, buffer, num_bytes.
 *
 * \retval >0                   Number of bytes copied.
 * \retval 0                    There was no remaining data in this input
 *                              vector.
 * \retval "Does not return"    The call is invalid, one or more of the
 *                              following are true:
 * \arg                           msg_handle is invalid.
 * \arg                           msg_handle does not refer to a
 *                                \ref PSA_IPC_CALL message.
 * \arg                           invec_idx is equal to or greater than
 *                                \ref PSA_MAX_IOVEC.
 * \arg                           the memory reference for buffer is invalid or
 *                                not writable.
 */
static size_t tfm_svcall_psa_read(uint32_t *args)
{
    psa_handle_t msg_handle;
    uint32_t invec_idx;
    void *buffer = NULL;
    size_t num_bytes;
    size_t bytes;
    struct tfm_msg_body_t *msg = NULL;

    TFM_ASSERT(args != NULL);
    msg_handle = (psa_handle_t)args[0];
    invec_idx = args[1];
    buffer = (void *)args[2];
    num_bytes = (size_t)args[3];

    /* It is a fatal error if message handle is invalid */
    msg = tfm_spm_get_msg_from_handle(msg_handle);
    if (!msg) {
        tfm_panic();
    }

    /*
     * It is a fatal error if message handle does not refer to a PSA_IPC_CALL
     * message
     */
    if (msg->msg.type != PSA_IPC_CALL) {
        tfm_panic();
    }

    /*
     * It is a fatal error if invec_idx is equal to or greater than
     * PSA_MAX_IOVEC
     */
    if (invec_idx >= PSA_MAX_IOVEC) {
        tfm_panic();
    }

    /* There was no remaining data in this input vector */
    if (msg->msg.in_size[invec_idx] == 0) {
        return 0;
    }

    /*
     * It is a fatal error if the memory reference for buffer is invalid or
     * not writable
     */
    /* FixMe: write permission check to be added */
    if (tfm_memory_check(buffer, num_bytes, false) != IPC_SUCCESS) {
        tfm_panic();
    }

    bytes = num_bytes > msg->msg.in_size[invec_idx] ?
                        msg->msg.in_size[invec_idx] : num_bytes;

    tfm_memcpy(buffer, msg->invec[invec_idx].base, bytes);

    /* There maybe some remaining data */
    msg->invec[invec_idx].base += bytes;
    msg->msg.in_size[invec_idx] -= bytes;

    return bytes;
}

/**
 * \brief SVC handler for \ref psa_skip.
 *
 * \param[in] args              Include all input arguments:
 *                              msg_handle, invec_idx, num_bytes.
 *
 * \retval >0                   Number of bytes skipped.
 * \retval 0                    There was no remaining data in this input
 *                              vector.
 * \retval "Does not return"    The call is invalid, one or more of the
 *                              following are true:
 * \arg                           msg_handle is invalid.
 * \arg                           msg_handle does not refer to a
 *                                \ref PSA_IPC_CALL message.
 * \arg                           invec_idx is equal to or greater than
 *                                \ref PSA_MAX_IOVEC.
 */
static size_t tfm_svcall_psa_skip(uint32_t *args)
{
    psa_handle_t msg_handle;
    uint32_t invec_idx;
    size_t num_bytes;
    struct tfm_msg_body_t *msg = NULL;

    TFM_ASSERT(args != NULL);
    msg_handle = (psa_handle_t)args[0];
    invec_idx = args[1];
    num_bytes = (size_t)args[2];

    /* It is a fatal error if message handle is invalid */
    msg = tfm_spm_get_msg_from_handle(msg_handle);
    if (!msg) {
        tfm_panic();
    }

    /*
     * It is a fatal error if message handle does not refer to a PSA_IPC_CALL
     * message
     */
    if (msg->msg.type != PSA_IPC_CALL) {
        tfm_panic();
    }

    /*
     * It is a fatal error if invec_idx is equal to or greater than
     * PSA_MAX_IOVEC
     */
    if (invec_idx >= PSA_MAX_IOVEC) {
        tfm_panic();
    }

    /* There was no remaining data in this input vector */
    if (msg->msg.in_size[invec_idx] == 0) {
        return 0;
    }

    /*
     * If num_bytes is greater than the remaining size of the input vector then
     * the remaining size of the input vector is used.
     */
    if (num_bytes > msg->msg.in_size[invec_idx]) {
        num_bytes = msg->msg.in_size[invec_idx];
    }

    /* There maybe some remaining data */
    msg->invec[invec_idx].base += num_bytes;
    msg->msg.in_size[invec_idx] -= num_bytes;

    return num_bytes;
}

/**
 * \brief SVC handler for \ref psa_write.
 *
 * \param[in] args              Include all input arguments:
 *                              msg_handle, outvec_idx, buffer, num_bytes.
 *
 * \retval void                 Success
 * \retval "Does not return"    The call is invalid, one or more of the
 *                              following are true:
 * \arg                           msg_handle is invalid.
 * \arg                           msg_handle does not refer to a
 *                                \ref PSA_IPC_CALL message.
 * \arg                           outvec_idx is equal to or greater than
 *                                \ref PSA_MAX_IOVEC.
 * \arg                           The memory reference for buffer is invalid.
 * \arg                           The call attempts to write data past the end
 *                                of the client output vector.
 */
static void tfm_svcall_psa_write(uint32_t *args)
{
    psa_handle_t msg_handle;
    uint32_t outvec_idx;
    void *buffer = NULL;
    size_t num_bytes;
    struct tfm_msg_body_t *msg = NULL;

    TFM_ASSERT(args != NULL);
    msg_handle = (psa_handle_t)args[0];
    outvec_idx = args[1];
    buffer = (void *)args[2];
    num_bytes = (size_t)args[3];

    /* It is a fatal error if message handle is invalid */
    msg = tfm_spm_get_msg_from_handle(msg_handle);
    if (!msg) {
        tfm_panic();
    }

    /*
     * It is a fatal error if message handle does not refer to a PSA_IPC_CALL
     * message
     */
    if (msg->msg.type != PSA_IPC_CALL) {
        tfm_panic();
    }

    /*
     * It is a fatal error if outvec_idx is equal to or greater than
     * PSA_MAX_IOVEC
     */
    if (outvec_idx >= PSA_MAX_IOVEC) {
        tfm_panic();
    }

    /*
     * It is a fatal error if the call attempts to write data past the end of
     * the client output vector
     */
    if (num_bytes > msg->msg.out_size[outvec_idx] -
        msg->outvec[outvec_idx].len) {
        tfm_panic();
    }

    /* It is a fatal error if the memory reference for buffer is valid */
    if (tfm_memory_check(buffer, num_bytes, false) != IPC_SUCCESS) {
        tfm_panic();
    }

    tfm_memcpy(msg->outvec[outvec_idx].base + msg->outvec[outvec_idx].len,
               buffer, num_bytes);

    /* Update the write number */
    msg->outvec[outvec_idx].len += num_bytes;
}

static void update_caller_outvec_len(struct tfm_msg_body_t *msg)
{
    int32_t i = 0;

    /*
     * FixeMe: abstract these part into dedicated functions to avoid
     * accessing thread context in psa layer
     */
    TFM_ASSERT(msg->ack_mtx.owner->status == THRD_STAT_BLOCK);

    while (msg->msg.out_size[i] != 0) {
        TFM_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
        msg->caller_outvec[i].len = msg->outvec[i].len;
        i++;
    }
}
/**
 * \brief SVC handler for \ref psa_reply.
 *
 * \param[in] args              Include all input arguments:
 *                              msg_handle, status.
 *
 * \retval void                 Success.
 * \retval "Does not return"    The call is invalid, one or more of the
 *                              following are true:
 * \arg                         msg_handle is invalid.
 * \arg                         An invalid status code is specified for the
 *                              type of message.
 */
static void tfm_svcall_psa_reply(uint32_t *args)
{
    psa_handle_t msg_handle;
    psa_status_t status;
    struct tfm_spm_service_t *service = NULL;
    struct tfm_msg_body_t *msg = NULL;
    psa_handle_t connect_handle;
    int32_t ret = PSA_SUCCESS;

    TFM_ASSERT(args != NULL);
    msg_handle = (psa_handle_t)args[0];
    status = (psa_status_t)args[1];

    /* It is a fatal error if message handle is invalid */
    msg = tfm_spm_get_msg_from_handle(msg_handle);
    if (!msg) {
        tfm_panic();
    }

    /*
     * RoT Service information is needed in this function, stored it in message
     * body structure. Only two parameters are passed in this function: handle
     * and status, so it is useful and simply to do like this.
     */
    service = msg->service;
    if (!service) {
        tfm_panic();
    }

    /*
     * Three type of message are passed in this function: CONNECT, CALL,
     * DISCONNECT. It needs to process differently for each type.
     */
    switch (msg->msg.type) {
    case PSA_IPC_CONNECT:
        /*
         * Reply to PSA_IPC_CONNECT message. Connect handle is created if the
         * input status is PSA_SUCCESS. Others return values are based on the
         * input status.
         */
        if (status == PSA_SUCCESS) {
            connect_handle = tfm_spm_create_conn_handle(service);
            if (connect_handle == PSA_NULL_HANDLE) {
                tfm_panic();
            }
            ret = connect_handle;
        } else if (status == PSA_CONNECTION_REFUSED) {
            ret = PSA_CONNECTION_REFUSED;
        } else if (status == PSA_CONNECTION_BUSY) {
            ret = PSA_CONNECTION_BUSY;
        } else {
            tfm_panic();
        }
        break;
    case PSA_IPC_CALL:
        /* Reply to PSA_IPC_CALL message. Return values are based on status */
        if (status == PSA_SUCCESS) {
            ret = PSA_SUCCESS;
        } else if (status == PSA_DROP_CONNECTION) {
            ret = PSA_DROP_CONNECTION;
        } else if ((status >= (INT32_MIN + 1)) &&
                   (status <= (INT32_MIN + 127))) {
            tfm_panic();
        } else if ((status >= (INT32_MIN + 128)) && (status <= -1)) {
            ret = status;
        } else if ((status >= 1) && (status <= INT32_MAX)) {
            ret = status;
        } else {
            tfm_panic();
        }

        /*
         * The total number of bytes written to a single parameter must be
         * reported to the client by updating the len member of the psa_outvec
         * structure for the parameter before returning from psa_call().
         */
        update_caller_outvec_len(msg);
        break;
    case PSA_IPC_DISCONNECT:
        /*
         * If the message type is PSA_IPC_DISCONNECT, then the status code is
         * ignored
         */
        break;
    default:
        tfm_panic();
    }

    /* Save return value for blocked threads */
    tfm_event_owner_retval(&msg->ack_mtx, ret);

    /* Wake waiting thread up */
    tfm_event_signal(&msg->ack_mtx);

    /* Message should not be unsed anymore */
    tfm_spm_free_msg(msg);
}

/**
 * \brief SVC handler for \ref psa_notify.
 *
 * \param[in] args              Include all input arguments: partition_id.
 *
 * \retval void                 Success.
 * \retval "Does not return"    partition_id does not correspond to a Secure
 *                              Partition.
 */
static void tfm_svcall_psa_notify(uint32_t *args)
{
    int32_t partition_id;
    struct tfm_spm_ipc_partition_t *partition = NULL;

    TFM_ASSERT(args != NULL);
    partition_id = (int32_t)args[0];

    /*
     * The value of partition_id must be greater than zero as the target of
     * notification must be a Secure Partition, providing a Non-secure
     * Partition ID is a fatal error.
     */
    if (!TFM_CLIENT_ID_IS_S(partition_id)) {
        tfm_panic();
    }

    /*
     * It is a fatal error if partition_id does not correspond to a Secure
     * Partition.
     */
    partition = tfm_spm_get_partition_by_id(partition_id);
    if (!partition) {
        tfm_panic();
    }

    partition->signals |= PSA_DOORBELL;

    /*
     * The target partition may be blocked with waiting for signals after
     * called psa_wait(). Set the return value with the available signals
     * before wake it up with tfm_event_signal().
     */
    tfm_event_owner_retval(&partition->signal_event,
                           partition->signals & partition->signal_mask);

    /* Wake waiting thread up */
    tfm_event_signal(&partition->signal_event);
}

/**
 * \brief SVC handler for \ref psa_clear.
 *
 * \retval void                 Success.
 * \retval "Does not return"    The Secure Partition's doorbell signal is not
 *                              currently asserted.
 */
static void tfm_svcall_psa_clear(uint32_t *args)
{
    struct tfm_spm_ipc_partition_t *partition = NULL;

    partition = tfm_spm_get_running_partition();
    if (!partition) {
        tfm_panic();
    }

    /*
     * It is a fatal error if the Secure Partition's doorbell signal is not
     * currently asserted.
     */
    if ((partition->signals & PSA_DOORBELL) == 0) {
        tfm_panic();
    }
    partition->signals &= ~PSA_DOORBELL;
}

/**
 * \brief SVC handler for \ref psa_eoi.
 *
 * \param[in] args              Include all input arguments: irq_signal.
 *
 * \retval void                 Success.
 * \retval "Does not return"    The call is invalid, one or more of the
 *                              following are true:
 * \arg                           irq_signal is not an interrupt signal.
 * \arg                           irq_signal indicates more than one signal.
 * \arg                           irq_signal is not currently asserted.
 */
static void tfm_svcall_psa_eoi(uint32_t *args)
{
    psa_signal_t irq_signal;
    struct tfm_spm_ipc_partition_t *partition = NULL;

    TFM_ASSERT(args != NULL);
    irq_signal = (psa_signal_t)args[0];

    partition = tfm_spm_get_running_partition();
    if (!partition) {
        tfm_panic();
    }

    /*
     * FixMe: It is a fatal error if passed signal is not an interrupt signal.
     */

    /* It is a fatal error if passed signal indicates more than one signals. */
    if (tfm_bitcount(partition->signals) != 1) {
        tfm_panic();
    }

    /* It is a fatal error if passed signal is not currently asserted */
    if ((partition->signals & irq_signal) == 0) {
        tfm_panic();
    }

    partition->signals &= ~irq_signal;

    /* FixMe: re-enable interrupt */
}

int32_t SVC_Handler_IPC(tfm_svc_number_t svc_num, uint32_t *ctx)
{
    switch (svc_num) {
    case TFM_SVC_SCHEDULE:
        tfm_thrd_activate_schedule();
        break;
    case TFM_SVC_PSA_FRAMEWORK_VERSION:
        return tfm_svcall_psa_framework_version();
    case TFM_SVC_PSA_VERSION:
        return tfm_svcall_psa_version(ctx, 0);
    case TFM_SVC_PSA_CONNECT:
        return tfm_svcall_psa_connect(ctx, 0);
    case TFM_SVC_PSA_CALL:
        return tfm_svcall_psa_call(ctx, 0);
    case TFM_SVC_PSA_CLOSE:
        tfm_svcall_psa_close(ctx, 0);
        break;
    case TFM_SVC_PSA_WAIT:
        return tfm_svcall_psa_wait(ctx);
    case TFM_SVC_PSA_GET:
        return tfm_svcall_psa_get(ctx);
    case TFM_SVC_PSA_SET_RHANDLE:
        tfm_svcall_psa_set_rhandle(ctx);
        break;
    case TFM_SVC_PSA_READ:
        return tfm_svcall_psa_read(ctx);
    case TFM_SVC_PSA_SKIP:
        return tfm_svcall_psa_skip(ctx);
    case TFM_SVC_PSA_WRITE:
        tfm_svcall_psa_write(ctx);
        break;
    case TFM_SVC_PSA_REPLY:
        tfm_svcall_psa_reply(ctx);
        break;
    case TFM_SVC_PSA_NOTIFY:
        tfm_svcall_psa_notify(ctx);
        break;
    case TFM_SVC_PSA_CLEAR:
        tfm_svcall_psa_clear(ctx);
        break;
    case TFM_SVC_PSA_EOI:
        tfm_svcall_psa_eoi(ctx);
        break;
    default:
        break;
    }
    return PSA_SUCCESS;
}