You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2020/05/02 16:15:54 UTC

[GitHub] [incubator-nuttx-apps] patacongo opened a new issue #227: Cannot write to errno in all build modes

patacongo opened a new issue #227:
URL: https://github.com/apache/incubator-nuttx-apps/issues/227


   Problem:  Currently, in user applications, the macro set_errno() should be used to access the errno variable.  In the FLAT build mode this will work:
   
       errno = errvalue;
   
   But that will not work in the PROTECTED or KERNEL build modes.  In those cases we must do:
   
       set_errno(errvalue);
   
   From apps/ PR #226 discussion:
   
   @patacongo
   
   Have you tried this in protected and/or kernel build modes. There might be problems when writing to the errno in those cases since the errno resides in the TCB which is not accessible. In those modes set_errno() is a function call that goes through a syscall and errno is defined to be get_errno() which will only support read access.
   
   So reading the errno in the modes is okay, but writing must explicitly use set_errno(). That does break the standard but I am open to other technical solutions.
   
   @xiaoxiang781216
   
   Yes, you are right. userspace can't access TCB directly in protected/kernel build.
   
   What I can imagine:
   1.Move errno to userspace(e.g. at the begining of thread stack)
   2.Overwrite assignment operatior(but require C++)
   
   @patacongo
   
       What I can imagine:
       1.Move errno to userspace(e.g. at the begining of thread stack)
   
   Except how would you find the beginning of the thread stack in user space?
   
   This is how TLS works. TLS aligns the stack and then, by masking the current stack pointer, you can get the address of the base of the current stack. However, this does not work in FLAT or PROTECTED modes. That is because the size of that mask also effects the maximum size of the stack. For example, if I use the a 4Kb mask 0x00000fff, then the maximum stack size would be 4Kb which is not big enough. Large stack alignments effect memory usage on platforms with minimal RAM resources.
   
   So TLS can only really be used in the KERNEL build mode which has highly aligned virtual stacks.
   
   In KERNEL mode, we could even do things as GLIBC does: errno is just a global variable in the process address space. It is unique to process, but not to a thread.
   
   We could do something like this by moving many of the user wrapper functions from the sched/ to libs/libc. For example: nxsem_wait() and sem_wait() are both in sched/semaphore/sem_wait.c. We could mov sem_wait to libs/libc/sempahores/lib_semwait.c. If we did that for EVERY OS interface, then the errno could be handled completely in user space and could be moved out of the TCB.
   
   !!! No that would not work because sem_wait() is also a cancellation point which would not work in user space in its current form.
   
       2.Overwrite assignment operatior(but require C++)
   
   Or use the non-standard set_errno();
   
   @patacongo
   
   I looked at how GLIBC handles errno. It uses TLS. Here is a general overview:
   
   glibc's errno.h has multiple definitions of errno.h but if nothing is defined, it includes stdlib/errno.h and stdlib/errno.h has this:
   
       /* The error code set by various library functions.  */
       extern int *__errno_location (void) __THROW __attribute_const__;
       # define errno (*__errno_location ())
   
   There are various implementations of __errno_location(). Most default to this:
   
       #include <errno.h>
       #include <tls.h>
       
       int *
       __errno_location (void)
       {
         return &errno;
       }
       libc_hidden_def (__errno_location)
   
   Where errno is then defined (errno.h) as a thread-specific integer:
   
       extern __thread int errno attribute_tls_model_ie;
   
   @patacongo
   
   I think that we could implement a different kind of TLS that does not require aligned stacks. We could so a system call to get the stack bas address. There is already a pthread_get_stackaddr_np(), we could generalize that more and use it to get the base of the stack and then provide an alternative implementation of TLS that used the non-aligned stack base.
   
   That would not be too difficult. However, it is still inefficient since we would have to do a system call to get the stack base address. With traditional TLS, we only have to mask the stack pointer address and that can all be done in user space.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on issue #227: Cannot write to errno in all build modes

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on issue #227:
URL: https://github.com/apache/incubator-nuttx-apps/issues/227#issuecomment-633411289


   Fixed by #238


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 edited a comment on issue #227: Cannot write to errno in all build modes

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 edited a comment on issue #227:
URL: https://github.com/apache/incubator-nuttx-apps/issues/227#issuecomment-633411289


   Fixed by #238 and https://github.com/apache/incubator-nuttx/pull/997


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 closed issue #227: Cannot write to errno in all build modes

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 closed issue #227:
URL: https://github.com/apache/incubator-nuttx-apps/issues/227


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org