what is the current execution mode/exception level, etc?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I am new to ARMv8 architecture. I have following basic questions on my mind:




  1. How do I know what is the current execution mode AArch32 or AArch64? Should I read CPSR or SPSR to ascertain this?


  2. What is the current Exception level, EL0/1/2/3?


  3. Once an exception comes, can i read any register to determine whether I am in Serror/Synchronous/IRQ/FIQ exception handler.



TIA.










share|improve this question


















  • 2





    "How do I know what is the current execution mode AArch32 or AArch64?" - I figure if the the code trying to check the mode is compiled for 64-bit, the mode is 64-bit; if it's compiled for 32-bit the mode is 32-bit.

    – JimmyB
    Aug 3 '15 at 12:59


















2















I am new to ARMv8 architecture. I have following basic questions on my mind:




  1. How do I know what is the current execution mode AArch32 or AArch64? Should I read CPSR or SPSR to ascertain this?


  2. What is the current Exception level, EL0/1/2/3?


  3. Once an exception comes, can i read any register to determine whether I am in Serror/Synchronous/IRQ/FIQ exception handler.



TIA.










share|improve this question


















  • 2





    "How do I know what is the current execution mode AArch32 or AArch64?" - I figure if the the code trying to check the mode is compiled for 64-bit, the mode is 64-bit; if it's compiled for 32-bit the mode is 32-bit.

    – JimmyB
    Aug 3 '15 at 12:59














2












2








2








I am new to ARMv8 architecture. I have following basic questions on my mind:




  1. How do I know what is the current execution mode AArch32 or AArch64? Should I read CPSR or SPSR to ascertain this?


  2. What is the current Exception level, EL0/1/2/3?


  3. Once an exception comes, can i read any register to determine whether I am in Serror/Synchronous/IRQ/FIQ exception handler.



TIA.










share|improve this question














I am new to ARMv8 architecture. I have following basic questions on my mind:




  1. How do I know what is the current execution mode AArch32 or AArch64? Should I read CPSR or SPSR to ascertain this?


  2. What is the current Exception level, EL0/1/2/3?


  3. Once an exception comes, can i read any register to determine whether I am in Serror/Synchronous/IRQ/FIQ exception handler.



TIA.







exception-handling arm arm64






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 3 '15 at 12:43









chetanchetan

498




498








  • 2





    "How do I know what is the current execution mode AArch32 or AArch64?" - I figure if the the code trying to check the mode is compiled for 64-bit, the mode is 64-bit; if it's compiled for 32-bit the mode is 32-bit.

    – JimmyB
    Aug 3 '15 at 12:59














  • 2





    "How do I know what is the current execution mode AArch32 or AArch64?" - I figure if the the code trying to check the mode is compiled for 64-bit, the mode is 64-bit; if it's compiled for 32-bit the mode is 32-bit.

    – JimmyB
    Aug 3 '15 at 12:59








2




2





"How do I know what is the current execution mode AArch32 or AArch64?" - I figure if the the code trying to check the mode is compiled for 64-bit, the mode is 64-bit; if it's compiled for 32-bit the mode is 32-bit.

– JimmyB
Aug 3 '15 at 12:59





"How do I know what is the current execution mode AArch32 or AArch64?" - I figure if the the code trying to check the mode is compiled for 64-bit, the mode is 64-bit; if it's compiled for 32-bit the mode is 32-bit.

– JimmyB
Aug 3 '15 at 12:59












2 Answers
2






active

oldest

votes


















5















  1. The assembly instructions and their binary encoding are entirely different for 32 and 64 bit. So the information what mode you are currently in is something that you/ the compiler already needs to know during compilation. checking for them at runtime doesn't make sense. For C, C++ checking can be done at compile time (#ifdef) through compiler provided macros like the ones provided by armclang: __aarch64__ for 64 bit, __arm__ for 32 bit

  2. depends on the execution mode:


    • aarch32: MRS <Rn>, CPSR read the current state into register number n. Then extract bits 3:0 that contain the current mode.

    • aarch64: MRS <Xn>, CurrentEL read the current EL into register number n



  3. short answer: you can't. long answer: the assumption is that by the structure of the code and the state of any user defined variables, you already know what you are doing. i.e. whether you came to a position in code through regular code or through an exception.






share|improve this answer
























  • For the 2nd point I am able to find the current mode when I am in EL1, EL2, or EL3 mode by reading through CPSR register. However, if I am in EL0 mode reading cpsr causes exception.

    – chetan
    May 4 '16 at 9:00











  • @chetan: "if I am in EL0 mode reading cpsr causes exception." AFAIK, that of course is the whole point! EL0 is the unprivileged 'app' mode; apps cannot/must not have access to info like this..So, my understanding is, write a kernel module to read register values. It should work as it runs in EL1, if not EL2.

    – kaiwan
    Jul 27 '17 at 6:30





















1














aarch64 C code:



register uint64_t x0 __asm__ ("x0");
__asm__ ("mrs x0, CurrentEL;" : : : "%x0");
printf("EL = %" PRIu64 "n", x0 >> 2);


arm C code:



register uint32_t r0 __asm__ ("r0");
__asm__ ("mrs r0, CPSR" : : : "%r0");
printf("EL = %" PRIu32 "n", r0 & 0x1F);


Those registers are normally (always?) protected in userland though, so you must to this from the Linux kernel or a baremetal example.



Tested on QEMU and gem5 with this setup.






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31787617%2fwhat-is-the-current-execution-mode-exception-level-etc%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5















    1. The assembly instructions and their binary encoding are entirely different for 32 and 64 bit. So the information what mode you are currently in is something that you/ the compiler already needs to know during compilation. checking for them at runtime doesn't make sense. For C, C++ checking can be done at compile time (#ifdef) through compiler provided macros like the ones provided by armclang: __aarch64__ for 64 bit, __arm__ for 32 bit

    2. depends on the execution mode:


      • aarch32: MRS <Rn>, CPSR read the current state into register number n. Then extract bits 3:0 that contain the current mode.

      • aarch64: MRS <Xn>, CurrentEL read the current EL into register number n



    3. short answer: you can't. long answer: the assumption is that by the structure of the code and the state of any user defined variables, you already know what you are doing. i.e. whether you came to a position in code through regular code or through an exception.






    share|improve this answer
























    • For the 2nd point I am able to find the current mode when I am in EL1, EL2, or EL3 mode by reading through CPSR register. However, if I am in EL0 mode reading cpsr causes exception.

      – chetan
      May 4 '16 at 9:00











    • @chetan: "if I am in EL0 mode reading cpsr causes exception." AFAIK, that of course is the whole point! EL0 is the unprivileged 'app' mode; apps cannot/must not have access to info like this..So, my understanding is, write a kernel module to read register values. It should work as it runs in EL1, if not EL2.

      – kaiwan
      Jul 27 '17 at 6:30


















    5















    1. The assembly instructions and their binary encoding are entirely different for 32 and 64 bit. So the information what mode you are currently in is something that you/ the compiler already needs to know during compilation. checking for them at runtime doesn't make sense. For C, C++ checking can be done at compile time (#ifdef) through compiler provided macros like the ones provided by armclang: __aarch64__ for 64 bit, __arm__ for 32 bit

    2. depends on the execution mode:


      • aarch32: MRS <Rn>, CPSR read the current state into register number n. Then extract bits 3:0 that contain the current mode.

      • aarch64: MRS <Xn>, CurrentEL read the current EL into register number n



    3. short answer: you can't. long answer: the assumption is that by the structure of the code and the state of any user defined variables, you already know what you are doing. i.e. whether you came to a position in code through regular code or through an exception.






    share|improve this answer
























    • For the 2nd point I am able to find the current mode when I am in EL1, EL2, or EL3 mode by reading through CPSR register. However, if I am in EL0 mode reading cpsr causes exception.

      – chetan
      May 4 '16 at 9:00











    • @chetan: "if I am in EL0 mode reading cpsr causes exception." AFAIK, that of course is the whole point! EL0 is the unprivileged 'app' mode; apps cannot/must not have access to info like this..So, my understanding is, write a kernel module to read register values. It should work as it runs in EL1, if not EL2.

      – kaiwan
      Jul 27 '17 at 6:30
















    5












    5








    5








    1. The assembly instructions and their binary encoding are entirely different for 32 and 64 bit. So the information what mode you are currently in is something that you/ the compiler already needs to know during compilation. checking for them at runtime doesn't make sense. For C, C++ checking can be done at compile time (#ifdef) through compiler provided macros like the ones provided by armclang: __aarch64__ for 64 bit, __arm__ for 32 bit

    2. depends on the execution mode:


      • aarch32: MRS <Rn>, CPSR read the current state into register number n. Then extract bits 3:0 that contain the current mode.

      • aarch64: MRS <Xn>, CurrentEL read the current EL into register number n



    3. short answer: you can't. long answer: the assumption is that by the structure of the code and the state of any user defined variables, you already know what you are doing. i.e. whether you came to a position in code through regular code or through an exception.






    share|improve this answer














    1. The assembly instructions and their binary encoding are entirely different for 32 and 64 bit. So the information what mode you are currently in is something that you/ the compiler already needs to know during compilation. checking for them at runtime doesn't make sense. For C, C++ checking can be done at compile time (#ifdef) through compiler provided macros like the ones provided by armclang: __aarch64__ for 64 bit, __arm__ for 32 bit

    2. depends on the execution mode:


      • aarch32: MRS <Rn>, CPSR read the current state into register number n. Then extract bits 3:0 that contain the current mode.

      • aarch64: MRS <Xn>, CurrentEL read the current EL into register number n



    3. short answer: you can't. long answer: the assumption is that by the structure of the code and the state of any user defined variables, you already know what you are doing. i.e. whether you came to a position in code through regular code or through an exception.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 3 '15 at 15:48









    Martin KeßlerMartin Keßler

    322138




    322138













    • For the 2nd point I am able to find the current mode when I am in EL1, EL2, or EL3 mode by reading through CPSR register. However, if I am in EL0 mode reading cpsr causes exception.

      – chetan
      May 4 '16 at 9:00











    • @chetan: "if I am in EL0 mode reading cpsr causes exception." AFAIK, that of course is the whole point! EL0 is the unprivileged 'app' mode; apps cannot/must not have access to info like this..So, my understanding is, write a kernel module to read register values. It should work as it runs in EL1, if not EL2.

      – kaiwan
      Jul 27 '17 at 6:30





















    • For the 2nd point I am able to find the current mode when I am in EL1, EL2, or EL3 mode by reading through CPSR register. However, if I am in EL0 mode reading cpsr causes exception.

      – chetan
      May 4 '16 at 9:00











    • @chetan: "if I am in EL0 mode reading cpsr causes exception." AFAIK, that of course is the whole point! EL0 is the unprivileged 'app' mode; apps cannot/must not have access to info like this..So, my understanding is, write a kernel module to read register values. It should work as it runs in EL1, if not EL2.

      – kaiwan
      Jul 27 '17 at 6:30



















    For the 2nd point I am able to find the current mode when I am in EL1, EL2, or EL3 mode by reading through CPSR register. However, if I am in EL0 mode reading cpsr causes exception.

    – chetan
    May 4 '16 at 9:00





    For the 2nd point I am able to find the current mode when I am in EL1, EL2, or EL3 mode by reading through CPSR register. However, if I am in EL0 mode reading cpsr causes exception.

    – chetan
    May 4 '16 at 9:00













    @chetan: "if I am in EL0 mode reading cpsr causes exception." AFAIK, that of course is the whole point! EL0 is the unprivileged 'app' mode; apps cannot/must not have access to info like this..So, my understanding is, write a kernel module to read register values. It should work as it runs in EL1, if not EL2.

    – kaiwan
    Jul 27 '17 at 6:30







    @chetan: "if I am in EL0 mode reading cpsr causes exception." AFAIK, that of course is the whole point! EL0 is the unprivileged 'app' mode; apps cannot/must not have access to info like this..So, my understanding is, write a kernel module to read register values. It should work as it runs in EL1, if not EL2.

    – kaiwan
    Jul 27 '17 at 6:30















    1














    aarch64 C code:



    register uint64_t x0 __asm__ ("x0");
    __asm__ ("mrs x0, CurrentEL;" : : : "%x0");
    printf("EL = %" PRIu64 "n", x0 >> 2);


    arm C code:



    register uint32_t r0 __asm__ ("r0");
    __asm__ ("mrs r0, CPSR" : : : "%r0");
    printf("EL = %" PRIu32 "n", r0 & 0x1F);


    Those registers are normally (always?) protected in userland though, so you must to this from the Linux kernel or a baremetal example.



    Tested on QEMU and gem5 with this setup.






    share|improve this answer






























      1














      aarch64 C code:



      register uint64_t x0 __asm__ ("x0");
      __asm__ ("mrs x0, CurrentEL;" : : : "%x0");
      printf("EL = %" PRIu64 "n", x0 >> 2);


      arm C code:



      register uint32_t r0 __asm__ ("r0");
      __asm__ ("mrs r0, CPSR" : : : "%r0");
      printf("EL = %" PRIu32 "n", r0 & 0x1F);


      Those registers are normally (always?) protected in userland though, so you must to this from the Linux kernel or a baremetal example.



      Tested on QEMU and gem5 with this setup.






      share|improve this answer




























        1












        1








        1







        aarch64 C code:



        register uint64_t x0 __asm__ ("x0");
        __asm__ ("mrs x0, CurrentEL;" : : : "%x0");
        printf("EL = %" PRIu64 "n", x0 >> 2);


        arm C code:



        register uint32_t r0 __asm__ ("r0");
        __asm__ ("mrs r0, CPSR" : : : "%r0");
        printf("EL = %" PRIu32 "n", r0 & 0x1F);


        Those registers are normally (always?) protected in userland though, so you must to this from the Linux kernel or a baremetal example.



        Tested on QEMU and gem5 with this setup.






        share|improve this answer















        aarch64 C code:



        register uint64_t x0 __asm__ ("x0");
        __asm__ ("mrs x0, CurrentEL;" : : : "%x0");
        printf("EL = %" PRIu64 "n", x0 >> 2);


        arm C code:



        register uint32_t r0 __asm__ ("r0");
        __asm__ ("mrs r0, CPSR" : : : "%r0");
        printf("EL = %" PRIu32 "n", r0 & 0x1F);


        Those registers are normally (always?) protected in userland though, so you must to this from the Linux kernel or a baremetal example.



        Tested on QEMU and gem5 with this setup.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 11:36

























        answered Nov 23 '18 at 11:30









        Ciro SantilliCiro Santilli

        760218




        760218






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31787617%2fwhat-is-the-current-execution-mode-exception-level-etc%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Paul Cézanne

            UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

            Angular material date-picker (MatDatepicker) auto completes the date on focus out