Pass struct to xv6 system call












0















I'm aware which we are not able to pass parameters to xv6 system call directly and we are forced to use it's built in methods.



But all examples and questions in this site is about how to send integer to system call. Which it's answer is using argint() method.



But my question is, is there anyway to pass "struct" to a xv6 system call? Are there any bulit-in methods for this purpose too?



If there is, could you please say a simple example?










share|improve this question





























    0















    I'm aware which we are not able to pass parameters to xv6 system call directly and we are forced to use it's built in methods.



    But all examples and questions in this site is about how to send integer to system call. Which it's answer is using argint() method.



    But my question is, is there anyway to pass "struct" to a xv6 system call? Are there any bulit-in methods for this purpose too?



    If there is, could you please say a simple example?










    share|improve this question



























      0












      0








      0








      I'm aware which we are not able to pass parameters to xv6 system call directly and we are forced to use it's built in methods.



      But all examples and questions in this site is about how to send integer to system call. Which it's answer is using argint() method.



      But my question is, is there anyway to pass "struct" to a xv6 system call? Are there any bulit-in methods for this purpose too?



      If there is, could you please say a simple example?










      share|improve this question
















      I'm aware which we are not able to pass parameters to xv6 system call directly and we are forced to use it's built in methods.



      But all examples and questions in this site is about how to send integer to system call. Which it's answer is using argint() method.



      But my question is, is there anyway to pass "struct" to a xv6 system call? Are there any bulit-in methods for this purpose too?



      If there is, could you please say a simple example?







      c unix system-calls xv6






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 7:32







      Ramtin Mousavi

















      asked Nov 19 '18 at 23:08









      Ramtin MousaviRamtin Mousavi

      568




      568
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Passing a struct through system call is possible.



          While one can't pass a struct itself as a system call parameter, passing a pointer to it is possible and will allow using it as both an input or output parameter.



          Allowing to use as argument the data itself and not a pointer to it will damage the requirement of the system calls mechanism- as passing data must be implemented in a generic way to allow all data types to (as well as future structs) be used.



          Let's have a look on an existing implementation of the system call fstat.



          int fstat(int fd, struct stat *st);


          fstat requires a file descriptor number as an input and outputs a matching stats information using struct stat.



          struct stat {
          short type; // Type of file
          int dev; // File system's disk device
          uint ino; // Inode number
          short nlink; // Number of links to file
          uint size; // Size of file in bytes
          };


          Although fstat uses a struct pointer as an output parameter, using it as an input will be similar.



          The function sys_fstat in kernel code starts the implementation of fstat system call (XV6's convention is to handle parameter fetching from user space by sys_* functions).



          int sys_fstat(void)
          {
          struct file *f;
          struct stat *st;

          if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0)
          return -1;
          return filestat(f, st);
          }


          This function first gets a corresponding struct file to the file descriptor number received by the first fstat function argument (using argfd). Then, fetches the struct stat pointer received by the second fstat function argument using argptr and saves the given pointer in a local (function scope) pointer variable.



          At this point, all arguments were fetched and can be used by the kernel implementation.



          Note: Although the struct stat pointer is a user-space pointer (located on the lower half of the virtual space), it is safe for the kernel to use it here because when the kernel is serving a process' system call, it uses the process' own paging table.






          share|improve this answer
























          • thank's, i saw your answer after my projects deadline but it's the correct solution and i used this way too.

            – Ramtin Mousavi
            Nov 24 '18 at 16:50



















          0














          Although the above answer is correct but i prefered to write my own solutions to make it more usable for other viwers.



          i used argptr to pass a pointer-to-struct to my system call.



          in sysproc.c:



          int sys_counts (void){

          struct countTable *ct;
          argptr (0 , (void*)&ct ,sizeof(*ct));
          return counts(ct);
          }


          in proc.c:



          int counts (struct countTable *ct){


          for (int i=0 ; i<22 ; i++){
          (ct->system_calls)[i] = count_calls[i] ;
          }


          return 22;
          }


          and finally in my user-space-program:



          int main (){

          struct countTable *ct = malloc (sizeof (struct countTable));

          // call system call
          counts(ct);


          exit();
          }





          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%2f53383938%2fpass-struct-to-xv6-system-call%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









            0














            Passing a struct through system call is possible.



            While one can't pass a struct itself as a system call parameter, passing a pointer to it is possible and will allow using it as both an input or output parameter.



            Allowing to use as argument the data itself and not a pointer to it will damage the requirement of the system calls mechanism- as passing data must be implemented in a generic way to allow all data types to (as well as future structs) be used.



            Let's have a look on an existing implementation of the system call fstat.



            int fstat(int fd, struct stat *st);


            fstat requires a file descriptor number as an input and outputs a matching stats information using struct stat.



            struct stat {
            short type; // Type of file
            int dev; // File system's disk device
            uint ino; // Inode number
            short nlink; // Number of links to file
            uint size; // Size of file in bytes
            };


            Although fstat uses a struct pointer as an output parameter, using it as an input will be similar.



            The function sys_fstat in kernel code starts the implementation of fstat system call (XV6's convention is to handle parameter fetching from user space by sys_* functions).



            int sys_fstat(void)
            {
            struct file *f;
            struct stat *st;

            if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0)
            return -1;
            return filestat(f, st);
            }


            This function first gets a corresponding struct file to the file descriptor number received by the first fstat function argument (using argfd). Then, fetches the struct stat pointer received by the second fstat function argument using argptr and saves the given pointer in a local (function scope) pointer variable.



            At this point, all arguments were fetched and can be used by the kernel implementation.



            Note: Although the struct stat pointer is a user-space pointer (located on the lower half of the virtual space), it is safe for the kernel to use it here because when the kernel is serving a process' system call, it uses the process' own paging table.






            share|improve this answer
























            • thank's, i saw your answer after my projects deadline but it's the correct solution and i used this way too.

              – Ramtin Mousavi
              Nov 24 '18 at 16:50
















            0














            Passing a struct through system call is possible.



            While one can't pass a struct itself as a system call parameter, passing a pointer to it is possible and will allow using it as both an input or output parameter.



            Allowing to use as argument the data itself and not a pointer to it will damage the requirement of the system calls mechanism- as passing data must be implemented in a generic way to allow all data types to (as well as future structs) be used.



            Let's have a look on an existing implementation of the system call fstat.



            int fstat(int fd, struct stat *st);


            fstat requires a file descriptor number as an input and outputs a matching stats information using struct stat.



            struct stat {
            short type; // Type of file
            int dev; // File system's disk device
            uint ino; // Inode number
            short nlink; // Number of links to file
            uint size; // Size of file in bytes
            };


            Although fstat uses a struct pointer as an output parameter, using it as an input will be similar.



            The function sys_fstat in kernel code starts the implementation of fstat system call (XV6's convention is to handle parameter fetching from user space by sys_* functions).



            int sys_fstat(void)
            {
            struct file *f;
            struct stat *st;

            if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0)
            return -1;
            return filestat(f, st);
            }


            This function first gets a corresponding struct file to the file descriptor number received by the first fstat function argument (using argfd). Then, fetches the struct stat pointer received by the second fstat function argument using argptr and saves the given pointer in a local (function scope) pointer variable.



            At this point, all arguments were fetched and can be used by the kernel implementation.



            Note: Although the struct stat pointer is a user-space pointer (located on the lower half of the virtual space), it is safe for the kernel to use it here because when the kernel is serving a process' system call, it uses the process' own paging table.






            share|improve this answer
























            • thank's, i saw your answer after my projects deadline but it's the correct solution and i used this way too.

              – Ramtin Mousavi
              Nov 24 '18 at 16:50














            0












            0








            0







            Passing a struct through system call is possible.



            While one can't pass a struct itself as a system call parameter, passing a pointer to it is possible and will allow using it as both an input or output parameter.



            Allowing to use as argument the data itself and not a pointer to it will damage the requirement of the system calls mechanism- as passing data must be implemented in a generic way to allow all data types to (as well as future structs) be used.



            Let's have a look on an existing implementation of the system call fstat.



            int fstat(int fd, struct stat *st);


            fstat requires a file descriptor number as an input and outputs a matching stats information using struct stat.



            struct stat {
            short type; // Type of file
            int dev; // File system's disk device
            uint ino; // Inode number
            short nlink; // Number of links to file
            uint size; // Size of file in bytes
            };


            Although fstat uses a struct pointer as an output parameter, using it as an input will be similar.



            The function sys_fstat in kernel code starts the implementation of fstat system call (XV6's convention is to handle parameter fetching from user space by sys_* functions).



            int sys_fstat(void)
            {
            struct file *f;
            struct stat *st;

            if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0)
            return -1;
            return filestat(f, st);
            }


            This function first gets a corresponding struct file to the file descriptor number received by the first fstat function argument (using argfd). Then, fetches the struct stat pointer received by the second fstat function argument using argptr and saves the given pointer in a local (function scope) pointer variable.



            At this point, all arguments were fetched and can be used by the kernel implementation.



            Note: Although the struct stat pointer is a user-space pointer (located on the lower half of the virtual space), it is safe for the kernel to use it here because when the kernel is serving a process' system call, it uses the process' own paging table.






            share|improve this answer













            Passing a struct through system call is possible.



            While one can't pass a struct itself as a system call parameter, passing a pointer to it is possible and will allow using it as both an input or output parameter.



            Allowing to use as argument the data itself and not a pointer to it will damage the requirement of the system calls mechanism- as passing data must be implemented in a generic way to allow all data types to (as well as future structs) be used.



            Let's have a look on an existing implementation of the system call fstat.



            int fstat(int fd, struct stat *st);


            fstat requires a file descriptor number as an input and outputs a matching stats information using struct stat.



            struct stat {
            short type; // Type of file
            int dev; // File system's disk device
            uint ino; // Inode number
            short nlink; // Number of links to file
            uint size; // Size of file in bytes
            };


            Although fstat uses a struct pointer as an output parameter, using it as an input will be similar.



            The function sys_fstat in kernel code starts the implementation of fstat system call (XV6's convention is to handle parameter fetching from user space by sys_* functions).



            int sys_fstat(void)
            {
            struct file *f;
            struct stat *st;

            if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0)
            return -1;
            return filestat(f, st);
            }


            This function first gets a corresponding struct file to the file descriptor number received by the first fstat function argument (using argfd). Then, fetches the struct stat pointer received by the second fstat function argument using argptr and saves the given pointer in a local (function scope) pointer variable.



            At this point, all arguments were fetched and can be used by the kernel implementation.



            Note: Although the struct stat pointer is a user-space pointer (located on the lower half of the virtual space), it is safe for the kernel to use it here because when the kernel is serving a process' system call, it uses the process' own paging table.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 7:22









            Omer EfratOmer Efrat

            313




            313













            • thank's, i saw your answer after my projects deadline but it's the correct solution and i used this way too.

              – Ramtin Mousavi
              Nov 24 '18 at 16:50



















            • thank's, i saw your answer after my projects deadline but it's the correct solution and i used this way too.

              – Ramtin Mousavi
              Nov 24 '18 at 16:50

















            thank's, i saw your answer after my projects deadline but it's the correct solution and i used this way too.

            – Ramtin Mousavi
            Nov 24 '18 at 16:50





            thank's, i saw your answer after my projects deadline but it's the correct solution and i used this way too.

            – Ramtin Mousavi
            Nov 24 '18 at 16:50













            0














            Although the above answer is correct but i prefered to write my own solutions to make it more usable for other viwers.



            i used argptr to pass a pointer-to-struct to my system call.



            in sysproc.c:



            int sys_counts (void){

            struct countTable *ct;
            argptr (0 , (void*)&ct ,sizeof(*ct));
            return counts(ct);
            }


            in proc.c:



            int counts (struct countTable *ct){


            for (int i=0 ; i<22 ; i++){
            (ct->system_calls)[i] = count_calls[i] ;
            }


            return 22;
            }


            and finally in my user-space-program:



            int main (){

            struct countTable *ct = malloc (sizeof (struct countTable));

            // call system call
            counts(ct);


            exit();
            }





            share|improve this answer






























              0














              Although the above answer is correct but i prefered to write my own solutions to make it more usable for other viwers.



              i used argptr to pass a pointer-to-struct to my system call.



              in sysproc.c:



              int sys_counts (void){

              struct countTable *ct;
              argptr (0 , (void*)&ct ,sizeof(*ct));
              return counts(ct);
              }


              in proc.c:



              int counts (struct countTable *ct){


              for (int i=0 ; i<22 ; i++){
              (ct->system_calls)[i] = count_calls[i] ;
              }


              return 22;
              }


              and finally in my user-space-program:



              int main (){

              struct countTable *ct = malloc (sizeof (struct countTable));

              // call system call
              counts(ct);


              exit();
              }





              share|improve this answer




























                0












                0








                0







                Although the above answer is correct but i prefered to write my own solutions to make it more usable for other viwers.



                i used argptr to pass a pointer-to-struct to my system call.



                in sysproc.c:



                int sys_counts (void){

                struct countTable *ct;
                argptr (0 , (void*)&ct ,sizeof(*ct));
                return counts(ct);
                }


                in proc.c:



                int counts (struct countTable *ct){


                for (int i=0 ; i<22 ; i++){
                (ct->system_calls)[i] = count_calls[i] ;
                }


                return 22;
                }


                and finally in my user-space-program:



                int main (){

                struct countTable *ct = malloc (sizeof (struct countTable));

                // call system call
                counts(ct);


                exit();
                }





                share|improve this answer















                Although the above answer is correct but i prefered to write my own solutions to make it more usable for other viwers.



                i used argptr to pass a pointer-to-struct to my system call.



                in sysproc.c:



                int sys_counts (void){

                struct countTable *ct;
                argptr (0 , (void*)&ct ,sizeof(*ct));
                return counts(ct);
                }


                in proc.c:



                int counts (struct countTable *ct){


                for (int i=0 ; i<22 ; i++){
                (ct->system_calls)[i] = count_calls[i] ;
                }


                return 22;
                }


                and finally in my user-space-program:



                int main (){

                struct countTable *ct = malloc (sizeof (struct countTable));

                // call system call
                counts(ct);


                exit();
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 29 '18 at 9:18

























                answered Nov 24 '18 at 16:58









                Ramtin MousaviRamtin Mousavi

                568




                568






























                    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%2f53383938%2fpass-struct-to-xv6-system-call%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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini