Subversion Repositories HelenOS

Rev

Rev 3386 | Rev 4327 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3386 Rev 4153
Line 40... Line 40...
40
#include <debug.h>
40
#include <debug.h>
41
#include <ipc/ipc.h>
41
#include <ipc/ipc.h>
42
#include <ipc/sysipc.h>
42
#include <ipc/sysipc.h>
43
#include <ipc/irq.h>
43
#include <ipc/irq.h>
44
#include <ipc/ipcrsc.h>
44
#include <ipc/ipcrsc.h>
-
 
45
#include <ipc/kbox.h>
-
 
46
#include <udebug/udebug_ipc.h>
45
#include <arch/interrupt.h>
47
#include <arch/interrupt.h>
46
#include <print.h>
-
 
47
#include <syscall/copy.h>
48
#include <syscall/copy.h>
48
#include <security/cap.h>
49
#include <security/cap.h>
49
#include <mm/as.h>
50
#include <mm/as.h>
50
#include <print.h>
51
#include <print.h>
51
 
52
 
Line 293... Line 294...
293
}
294
}
294
 
295
 
295
/** Called before the request is sent.
296
/** Called before the request is sent.
296
 *
297
 *
297
 * @param call      Call structure with the request.
298
 * @param call      Call structure with the request.
-
 
299
 * @param phone     Phone that the call will be sent through.
298
 *
300
 *
299
 * @return      Return 0 on success, ELIMIT or EPERM on error.
301
 * @return      Return 0 on success, ELIMIT or EPERM on error.
300
 */
302
 */
301
static int request_preprocess(call_t *call)
303
static int request_preprocess(call_t *call, phone_t *phone)
302
{
304
{
303
    int newphid;
305
    int newphid;
304
    size_t size;
306
    size_t size;
305
    uintptr_t src;
307
    uintptr_t src;
306
    int rc;
308
    int rc;
Line 338... Line 340...
338
        if (rc != 0) {
340
        if (rc != 0) {
339
            free(call->buffer);
341
            free(call->buffer);
340
            return rc;
342
            return rc;
341
        }
343
        }
342
        break;
344
        break;
-
 
345
#ifdef CONFIG_UDEBUG
-
 
346
    case IPC_M_DEBUG_ALL:
-
 
347
        return udebug_request_preprocess(call, phone);
-
 
348
#endif
343
    default:
349
    default:
344
        break;
350
        break;
345
    }
351
    }
346
    return 0;
352
    return 0;
347
}
353
}
Line 367... Line 373...
367
            IPC_SET_ARG5(call->data, call->priv);
373
            IPC_SET_ARG5(call->data, call->priv);
368
    }
374
    }
369
 
375
 
370
    if (call->buffer) {
376
    if (call->buffer) {
371
        /* This must be an affirmative answer to IPC_M_DATA_READ. */
377
        /* This must be an affirmative answer to IPC_M_DATA_READ. */
-
 
378
        /* or IPC_M_DEBUG_ALL/UDEBUG_M_MEM_READ... */
372
        uintptr_t dst = IPC_GET_ARG1(call->data);
379
        uintptr_t dst = IPC_GET_ARG1(call->data);
373
        size_t size = IPC_GET_ARG2(call->data);
380
        size_t size = IPC_GET_ARG2(call->data);
374
        int rc = copy_to_uspace((void *) dst, call->buffer, size);
381
        int rc = copy_to_uspace((void *) dst, call->buffer, size);
375
        if (rc)
382
        if (rc)
376
            IPC_SET_RETVAL(call->data, rc);
383
            IPC_SET_RETVAL(call->data, rc);
Line 397... Line 404...
397
            IPC_SET_RETVAL(call->data, ELIMIT);
404
            IPC_SET_RETVAL(call->data, ELIMIT);
398
            ipc_answer(box, call);
405
            ipc_answer(box, call);
399
            return -1;
406
            return -1;
400
        }
407
        }
401
        IPC_SET_ARG5(call->data, phoneid);
408
        IPC_SET_ARG5(call->data, phoneid);
402
    }
409
    }
-
 
410
    switch (IPC_GET_METHOD(call->data)) {
-
 
411
    case IPC_M_DEBUG_ALL:
-
 
412
        return -1;
-
 
413
    default:
-
 
414
        break;
-
 
415
    }
403
    return 0;
416
    return 0;
404
}
417
}
405
 
418
 
406
/** Make a fast call over IPC, wait for reply and return to user.
419
/** Make a fast call over IPC, wait for reply and return to user.
407
 *
420
 *
Line 439... Line 452...
439
     * the limits of the fast version.
452
     * the limits of the fast version.
440
     */
453
     */
441
    IPC_SET_ARG4(call.data, 0);
454
    IPC_SET_ARG4(call.data, 0);
442
    IPC_SET_ARG5(call.data, 0);
455
    IPC_SET_ARG5(call.data, 0);
443
 
456
 
444
    if (!(res = request_preprocess(&call))) {
457
    if (!(res = request_preprocess(&call, phone))) {
-
 
458
#ifdef CONFIG_UDEBUG
-
 
459
        udebug_stoppable_begin();
-
 
460
#endif
445
        rc = ipc_call_sync(phone, &call);
461
        rc = ipc_call_sync(phone, &call);
-
 
462
#ifdef CONFIG_UDEBUG
-
 
463
        udebug_stoppable_end();
-
 
464
#endif
446
        if (rc != EOK)
465
        if (rc != EOK)
447
            return rc;
466
            return rc;
448
        process_answer(&call);
467
        process_answer(&call);
-
 
468
 
449
    } else {
469
    } else {
450
        IPC_SET_RETVAL(call.data, res);
470
        IPC_SET_RETVAL(call.data, res);
451
    }
471
    }
452
    rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
472
    rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
453
    if (rc != 0)
473
    if (rc != 0)
Line 479... Line 499...
479
    if (rc != 0)
499
    if (rc != 0)
480
        return (unative_t) rc;
500
        return (unative_t) rc;
481
 
501
 
482
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
502
    GET_CHECK_PHONE(phone, phoneid, return ENOENT);
483
 
503
 
484
    if (!(res = request_preprocess(&call))) {
504
    if (!(res = request_preprocess(&call, phone))) {
-
 
505
#ifdef CONFIG_UDEBUG
-
 
506
        udebug_stoppable_begin();
-
 
507
#endif
485
        rc = ipc_call_sync(phone, &call);
508
        rc = ipc_call_sync(phone, &call);
-
 
509
#ifdef CONFIG_UDEBUG
-
 
510
        udebug_stoppable_end();
-
 
511
#endif
486
        if (rc != EOK)
512
        if (rc != EOK)
487
            return rc;
513
            return rc;
488
        process_answer(&call);
514
        process_answer(&call);
489
    } else
515
    } else
490
        IPC_SET_RETVAL(call.data, res);
516
        IPC_SET_RETVAL(call.data, res);
Line 548... Line 574...
548
     * To achieve deterministic behavior, zero out arguments that are beyond
574
     * To achieve deterministic behavior, zero out arguments that are beyond
549
     * the limits of the fast version.
575
     * the limits of the fast version.
550
     */
576
     */
551
    IPC_SET_ARG5(call->data, 0);
577
    IPC_SET_ARG5(call->data, 0);
552
 
578
 
553
    if (!(res = request_preprocess(call)))
579
    if (!(res = request_preprocess(call, phone)))
554
        ipc_call(phone, call);
580
        ipc_call(phone, call);
555
    else
581
    else
556
        ipc_backsend_err(phone, call, res);
582
        ipc_backsend_err(phone, call, res);
557
 
583
 
558
    return (unative_t) call;
584
    return (unative_t) call;
Line 582... Line 608...
582
        sizeof(call->data.args));
608
        sizeof(call->data.args));
583
    if (rc != 0) {
609
    if (rc != 0) {
584
        ipc_call_free(call);
610
        ipc_call_free(call);
585
        return (unative_t) rc;
611
        return (unative_t) rc;
586
    }
612
    }
587
    if (!(res = request_preprocess(call)))
613
    if (!(res = request_preprocess(call, phone)))
588
        ipc_call(phone, call);
614
        ipc_call(phone, call);
589
    else
615
    else
590
        ipc_backsend_err(phone, call, res);
616
        ipc_backsend_err(phone, call, res);
591
 
617
 
592
    return (unative_t) call;
618
    return (unative_t) call;
593
}
619
}
594
 
620
 
595
/** Forward a received call to another destination.
621
/** Forward a received call to another destination - common code for both the
-
 
622
 * fast and the slow version.
596
 *
623
 *
597
 * @param callid    Hash of the call to forward.
624
 * @param callid    Hash of the call to forward.
598
 * @param phoneid   Phone handle to use for forwarding.
625
 * @param phoneid   Phone handle to use for forwarding.
599
 * @param method    New method to use for the forwarded call.
626
 * @param method    New method to use for the forwarded call.
600
 * @param arg1      New value of the first argument for the forwarded call.
627
 * @param arg1      New value of the first argument for the forwarded call.
601
 * @param arg2      New value of the second argument for the forwarded call.
628
 * @param arg2      New value of the second argument for the forwarded call.
-
 
629
 * @param arg3      New value of the third argument for the forwarded call.
-
 
630
 * @param arg4      New value of the fourth argument for the forwarded call.
-
 
631
 * @param arg5      New value of the fifth argument for the forwarded call.
602
 * @param mode      Flags that specify mode of the forward operation.
632
 * @param mode      Flags that specify mode of the forward operation.
-
 
633
 * @param slow      If true, arg3, arg4 and arg5 are considered. Otherwise
-
 
634
 *          the function considers only the fast version arguments:
-
 
635
 *          i.e. arg1 and arg2.
603
 *
636
 *
604
 * @return      Return 0 on succes, otherwise return an error code.
637
 * @return      Return 0 on succes, otherwise return an error code.
605
 *
638
 *
606
 * In case the original method is a system method, ARG1, ARG2 and ARG3 are
-
 
607
 * overwritten in the forwarded message with the new method and the new arg1 and
-
 
608
 * arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are rewritten with
-
 
609
 * the new method, arg1 and arg2, respectively. Also note there is a set of
-
 
610
 * immutable methods, for which the new method and argument is not set and
-
 
611
 * these values are ignored.
-
 
612
 *
-
 
613
 * Warning: When implementing support for changing additional payload
-
 
614
 *      arguments, make sure that ARG5 is not rewritten for certain
639
 * Warning: Make sure that ARG5 is not rewritten for certain system IPC
615
 *      system IPC
-
 
616
 */
640
 */
617
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
641
static unative_t sys_ipc_forward_common(unative_t callid, unative_t phoneid,
618
    unative_t method, unative_t arg1, unative_t arg2, int mode)
642
    unative_t method, unative_t arg1, unative_t arg2, unative_t arg3,
-
 
643
    unative_t arg4, unative_t arg5, int mode, bool slow)
619
{
644
{
620
    call_t *call;
645
    call_t *call;
621
    phone_t *phone;
646
    phone_t *phone;
622
 
647
 
623
    call = get_call(callid);
648
    call = get_call(callid);
624
    if (!call)
649
    if (!call)
625
        return ENOENT;
650
        return ENOENT;
626
 
651
   
627
    call->flags |= IPC_CALL_FORWARDED;
652
    call->flags |= IPC_CALL_FORWARDED;
628
 
653
 
629
    GET_CHECK_PHONE(phone, phoneid, {
654
    GET_CHECK_PHONE(phone, phoneid, {
630
        IPC_SET_RETVAL(call->data, EFORWARD);
655
        IPC_SET_RETVAL(call->data, EFORWARD);
631
        ipc_answer(&TASK->answerbox, call);
656
        ipc_answer(&TASK->answerbox, call);
Line 638... Line 663...
638
        return EPERM;
663
        return EPERM;
639
    }
664
    }
640
 
665
 
641
    /*
666
    /*
642
     * Userspace is not allowed to change method of system methods on
667
     * Userspace is not allowed to change method of system methods on
643
     * forward, allow changing ARG1, ARG2 and ARG3 by means of method,
668
     * forward, allow changing ARG1, ARG2, ARG3 and ARG4 by means of method,
644
     * arg1 and arg2.
669
     * arg1, arg2 and arg3.
645
     * If the method is immutable, don't change anything.
670
     * If the method is immutable, don't change anything.
646
     */
671
     */
647
    if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
672
    if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
648
        if (method_is_system(IPC_GET_METHOD(call->data))) {
673
        if (method_is_system(IPC_GET_METHOD(call->data))) {
649
            if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
674
            if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
650
                phone_dealloc(IPC_GET_ARG5(call->data));
675
                phone_dealloc(IPC_GET_ARG5(call->data));
651
 
676
 
652
            IPC_SET_ARG1(call->data, method);
677
            IPC_SET_ARG1(call->data, method);
653
            IPC_SET_ARG2(call->data, arg1);
678
            IPC_SET_ARG2(call->data, arg1);
654
            IPC_SET_ARG3(call->data, arg2);
679
            IPC_SET_ARG3(call->data, arg2);
-
 
680
            if (slow) {
-
 
681
                IPC_SET_ARG4(call->data, arg3);
-
 
682
                /*
-
 
683
                 * For system methods we deliberately don't
-
 
684
                 * overwrite ARG5.
-
 
685
                 */
-
 
686
            }
655
        } else {
687
        } else {
656
            IPC_SET_METHOD(call->data, method);
688
            IPC_SET_METHOD(call->data, method);
657
            IPC_SET_ARG1(call->data, arg1);
689
            IPC_SET_ARG1(call->data, arg1);
658
            IPC_SET_ARG2(call->data, arg2);
690
            IPC_SET_ARG2(call->data, arg2);
-
 
691
            if (slow) {
-
 
692
                IPC_SET_ARG3(call->data, arg3);
-
 
693
                IPC_SET_ARG4(call->data, arg4);
-
 
694
                IPC_SET_ARG5(call->data, arg5);
-
 
695
            }
659
        }
696
        }
660
    }
697
    }
661
 
698
 
662
    return ipc_forward(call, phone, &TASK->answerbox, mode);
699
    return ipc_forward(call, phone, &TASK->answerbox, mode);
663
}
700
}
664
 
701
 
-
 
702
/** Forward a received call to another destination - fast version.
-
 
703
 *
-
 
704
 * @param callid    Hash of the call to forward.
-
 
705
 * @param phoneid   Phone handle to use for forwarding.
-
 
706
 * @param method    New method to use for the forwarded call.
-
 
707
 * @param arg1      New value of the first argument for the forwarded call.
-
 
708
 * @param arg2      New value of the second argument for the forwarded call.
-
 
709
 * @param mode      Flags that specify mode of the forward operation.
-
 
710
 *
-
 
711
 * @return      Return 0 on succes, otherwise return an error code.
-
 
712
 *
-
 
713
 * In case the original method is a system method, ARG1, ARG2 and ARG3 are
-
 
714
 * overwritten in the forwarded message with the new method and the new
-
 
715
 * arg1 and arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are
-
 
716
 * rewritten with the new method, arg1 and arg2, respectively. Also note there
-
 
717
 * is a set of immutable methods, for which the new method and arguments are not
-
 
718
 * set and these values are ignored.
-
 
719
 */
-
 
720
unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
-
 
721
    unative_t method, unative_t arg1, unative_t arg2, int mode)
-
 
722
{
-
 
723
    return sys_ipc_forward_common(callid, phoneid, method, arg1, arg2, 0, 0,
-
 
724
        0, mode, false);
-
 
725
}
-
 
726
 
-
 
727
/** Forward a received call to another destination - slow version.
-
 
728
 *
-
 
729
 * @param callid    Hash of the call to forward.
-
 
730
 * @param phoneid   Phone handle to use for forwarding.
-
 
731
 * @param data      Userspace address of the new IPC data.
-
 
732
 * @param mode      Flags that specify mode of the forward operation.
-
 
733
 *
-
 
734
 * @return      Return 0 on succes, otherwise return an error code.
-
 
735
 *
-
 
736
 * This function is the slow verision of the sys_ipc_forward_fast interface.
-
 
737
 * It can copy all five new arguments and the new method from the userspace.
-
 
738
 * It naturally extends the functionality of the fast version. For system
-
 
739
 * methods, it additionally stores the new value of arg3 to ARG4. For non-system
-
 
740
 * methods, it additionally stores the new value of arg3, arg4 and arg5,
-
 
741
 * respectively, to ARG3, ARG4 and ARG5, respectively.
-
 
742
 */
-
 
743
unative_t sys_ipc_forward_slow(unative_t callid, unative_t phoneid,
-
 
744
    ipc_data_t *data, int mode)
-
 
745
{
-
 
746
    ipc_data_t newdata;
-
 
747
    int rc;
-
 
748
 
-
 
749
    rc = copy_from_uspace(&newdata.args, &data->args,
-
 
750
        sizeof(newdata.args));
-
 
751
    if (rc != 0)
-
 
752
        return (unative_t) rc;
-
 
753
 
-
 
754
    return sys_ipc_forward_common(callid, phoneid,
-
 
755
        IPC_GET_METHOD(newdata), IPC_GET_ARG1(newdata),
-
 
756
        IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
-
 
757
        IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
-
 
758
}
-
 
759
 
665
/** Answer an IPC call - fast version.
760
/** Answer an IPC call - fast version.
666
 *
761
 *
667
 * This function can handle only two return arguments of payload, but is faster
762
 * This function can handle only two return arguments of payload, but is faster
668
 * than the generic sys_ipc_answer().
763
 * than the generic sys_ipc_answer().
669
 *
764
 *
Line 783... Line 878...
783
 */
878
 */
784
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
879
unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
785
{
880
{
786
    call_t *call;
881
    call_t *call;
787
 
882
 
788
restart:   
883
restart:
-
 
884
 
-
 
885
#ifdef CONFIG_UDEBUG
-
 
886
    udebug_stoppable_begin();
-
 
887
#endif  
789
    call = ipc_wait_for_call(&TASK->answerbox, usec,
888
    call = ipc_wait_for_call(&TASK->answerbox, usec,
790
        flags | SYNCH_FLAGS_INTERRUPTIBLE);
889
        flags | SYNCH_FLAGS_INTERRUPTIBLE);
-
 
890
 
-
 
891
#ifdef CONFIG_UDEBUG
-
 
892
    udebug_stoppable_end();
-
 
893
#endif
791
    if (!call)
894
    if (!call)
792
        return 0;
895
        return 0;
793
 
896
 
794
    if (call->flags & IPC_CALL_NOTIF) {
897
    if (call->flags & IPC_CALL_NOTIF) {
795
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
898
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
Line 807... Line 910...
807
    if (call->flags & IPC_CALL_ANSWERED) {
910
    if (call->flags & IPC_CALL_ANSWERED) {
808
        process_answer(call);
911
        process_answer(call);
809
 
912
 
810
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
913
        ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
811
 
914
 
812
        atomic_dec(&TASK->active_calls);
-
 
813
 
-
 
814
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
915
        if (call->flags & IPC_CALL_DISCARD_ANSWER) {
815
            ipc_call_free(call);
916
            ipc_call_free(call);
816
            goto restart;
917
            goto restart;
-
 
918
        } else {
-
 
919
            /*
-
 
920
             * Decrement the counter of active calls only if the
-
 
921
             * call is not an answer to IPC_M_PHONE_HUNGUP,
-
 
922
             * which doesn't contribute to the counter.
-
 
923
             */
-
 
924
            atomic_dec(&TASK->active_calls);
817
        }
925
        }
818
 
926
 
819
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
927
        STRUCT_TO_USPACE(&calldata->args, &call->data.args);
820
        ipc_call_free(call);
928
        ipc_call_free(call);
821
 
929
 
Line 826... Line 934...
826
        goto restart;
934
        goto restart;
827
 
935
 
828
    /* Include phone address('id') of the caller in the request,
936
    /* Include phone address('id') of the caller in the request,
829
     * copy whole call->data, not only call->data.args */
937
     * copy whole call->data, not only call->data.args */
830
    if (STRUCT_TO_USPACE(calldata, &call->data)) {
938
    if (STRUCT_TO_USPACE(calldata, &call->data)) {
-
 
939
        /*
-
 
940
         * The callee will not receive this call and no one else has
-
 
941
         * a chance to answer it. Reply with the EPARTY error code.
-
 
942
         */
-
 
943
        ipc_data_t saved_data;
-
 
944
        int saveddata = 0;
-
 
945
 
-
 
946
        if (answer_need_old(call)) {
-
 
947
            memcpy(&saved_data, &call->data, sizeof(call->data));
-
 
948
            saveddata = 1;
-
 
949
        }
-
 
950
       
-
 
951
        IPC_SET_RETVAL(call->data, EPARTY);
-
 
952
        (void) answer_preprocess(call, saveddata ? &saved_data : NULL);
-
 
953
        ipc_answer(&TASK->answerbox, call);
831
        return 0;
954
        return 0;
832
    }
955
    }
833
    return (unative_t)call;
956
    return (unative_t)call;
834
}
957
}
835
 
958
 
Line 866... Line 989...
866
    ipc_irq_unregister(&TASK->answerbox, inr, devno);
989
    ipc_irq_unregister(&TASK->answerbox, inr, devno);
867
 
990
 
868
    return 0;
991
    return 0;
869
}
992
}
870
 
993
 
-
 
994
#include <console/console.h>
-
 
995
 
-
 
996
/**
-
 
997
 * Syscall connect to a task by id.
-
 
998
 *
-
 
999
 * @return      Phone id on success, or negative error code.
-
 
1000
 */
-
 
1001
unative_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid_arg)
-
 
1002
{
-
 
1003
#ifdef CONFIG_UDEBUG
-
 
1004
    sysarg64_t taskid_arg;
-
 
1005
    int rc;
-
 
1006
   
-
 
1007
    rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
-
 
1008
    if (rc != 0)
-
 
1009
        return (unative_t) rc;
-
 
1010
 
-
 
1011
    LOG("sys_ipc_connect_kbox(%" PRIu64 ")\n", taskid_arg.value);
-
 
1012
 
-
 
1013
    return ipc_connect_kbox(taskid_arg.value);
-
 
1014
#else
-
 
1015
    return (unative_t) ENOTSUP;
-
 
1016
#endif
-
 
1017
}
-
 
1018
 
871
/** @}
1019
/** @}
872
 */
1020
 */