What is difference between __va() and phys_to_virt()? - linux

What is difference between __va() and phys_to_virt() ,what is need of these two separate implementation for same purpose, any difference between these two?

phys_to_virt and __va are preprocessor macros. phys_to_virt:
#if !defined(CONFIG_MMU)
#define virt_to_phys(address) ((unsigned long)(address))
#define phys_to_virt(address) ((void *)(address))
#else
#define virt_to_phys(address) (__pa(address))
#define phys_to_virt(address) (__va(address))
#endif
And __va
#define __va(x) ((void *)((unsigned long) (x)))
If CONFIG_MMU is not defined then are equals.

Related

Adding two strings together in the preprocessor

I am currently writing a header to make handling my external libs easier.
Here is the minimal code:
#pragma once
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define EXTERNAL_PATH STR(C:/C++ Libraries/)
#define LUA_PATH EXTERNAL_PATH STR(lua 5.3.4/)
#define LUA_INCLUDE LUA_PATH STR(include/)
So basically, I include this header and write something like
#include LUA_INCLUDE (add this two strings) "lua.hpp"
How can I link two strings in the preprocessor together ?
Are you looking for ##? And don't stringify (quote) macro params too early because then there is no way back.
Example:
#define MY_QUOTE(a) #a
#define CONCAT_QUOTE(a,b) MY_QUOTE(a##b)
#define CONCAT(a,b) a##b
// this works OK
#include CONCAT(<iostrea, m>)
// this doesn't as iostream should be just in <> rather than "<>"
#include CONCAT_QUOTE(<iostrea, m>)

arch/x86/include/asm/unistd.h vs. include/asm-generic/unistd.h

What's the difference between these two files? I can't really get it. I should mention that the first file should be arch/x86/include/asm/unistd_32.h (or and _64.h). Here is a quick preview of what they contain:
arch/x86/include/asm/unistd.h:
#ifndef _ASM_X86_UNISTD_32_H
#define _ASM_X86_UNISTD_32_H
/*
* This file contains the system call numbers.
*/
#define __NR_restart_syscall 0
#define __NR_exit 1
#define __NR_fork 2
#define __NR_read 3
#define __NR_write 4
#define __NR_open 5
#define __NR_close 6
#define __NR_waitpid 7
#define __NR_creat 8
#define __NR_link 9
#define __NR_unlink 10
#define __NR_execve 11
#define __NR_chdir 12
#define __NR_time 13
#define __NR_mknod 14
#define __NR_chmod 15
#define __NR_lchown 16
#define __NR_break 17
#define __NR_oldstat 18
#define __NR_lseek 19
#define __NR_getpid 20
#define __NR_mount 21
#define __NR_umount 22
include/asm-generic/unistd.h
#if !defined(_ASM_GENERIC_UNISTD_H) || defined(__SYSCALL)
#define _ASM_GENERIC_UNISTD_H
#include <asm/bitsperlong.h>
/*
* This file contains the system call numbers, based on the
* layout of the x86-64 architecture, which embeds the
* pointer to the syscall in the table.
*
* As a basic principle, no duplication of functionality
* should be added, e.g. we don't use lseek when llseek
* is present. New architectures should use this file
* and implement the less feature-full calls in user space.
*/
#ifndef __SYSCALL
#define __SYSCALL(x, y)
#endif
#if __BITS_PER_LONG == 32
#define __SC_3264(_nr, _32, _64) __SYSCALL(_nr, _32)
#else
#define __SC_3264(_nr, _32, _64) __SYSCALL(_nr, _64)
#endif
#define __NR_io_setup 0
__SYSCALL(__NR_io_setup, sys_io_setup)
#define __NR_io_destroy 1
__SYSCALL(__NR_io_destroy, sys_io_destroy)
#define __NR_io_submit 2
__SYSCALL(__NR_io_submit, sys_io_submit)
#define __NR_io_cancel 3
__SYSCALL(__NR_io_cancel, sys_io_cancel)
#define __NR_io_getevents 4
__SYSCALL(__NR_io_getevents, sys_io_getevents)
/* fs/xattr.c */
#define __NR_setxattr 5
__SYSCALL(__NR_setxattr, sys_setxattr)
#define __NR_lsetxattr 6
__SYSCALL(__NR_lsetxattr, sys_lsetxattr)
#define __NR_fsetxattr 7
__SYSCALL(__NR_fsetxattr, sys_fsetxattr)
#define __NR_getxattr 8
__SYSCALL(__NR_getxattr, sys_getxattr)
#define __NR_lgetxattr 9
__SYSCALL(__NR_lgetxattr, sys_lgetxattr)
#define __NR_fgetxattr 10
__SYSCALL(__NR_fgetxattr, sys_fgetxattr)
#define __NR_listxattr 11
__SYSCALL(__NR_listxattr, sys_listxattr)
#define __NR_llistxattr 12
I don't have a definite answer, but it's not uncommon for redundant files to exist when devs try to shift from old mechanisms to newer ones. Your case here looks quite similar.
If you checkout the 3.4 kernel, you will find that both arch/x86/include/asm/unistd_32.h and arch/x86/include/asm/unistd_64.h are gone. Instead, they are generated using arch/x86/syscalls.
Checkout the latest kernel (3.4.2 stable works for me), and do a "git log --stat arch/x86/include/asm", search for unistd_64.h or unistd_32.h or unistd.h.
I found the following commit might be interesting to you.
commit 303395ac3bf3e2cb488435537d416bc840438fcb
I've never touched syscalls before, so I'd rather not say too much. git log is usually how I sort out confusing files. You can also get into makefiles if you are good at it. (I am not, so I rely on git log. )

Linux kernel 0.0.1 vs 2.6.36, "ctype.h", whats the difference?

How efficient is linux kernel 2.6.36 from 0.0.1
0.0.1:
I understand, in this version, Linus has assigned the value of
c(input variable to a function) to a temp variable and operate on
that temp variable inside the function and return the temp variable.
This happens in tolower/toupper functions.
2.6.36:
Here in tolower/toupper he changed it.
ctype is const.
New function ismask is written.
What I want to know?:
Why these changes has been made?, what good it brought home?
I have listed out the ctype.h files in the kernel versions of 0.0.1 and 2.6.36, respectively.
**** ctype.h, linux-0.01 *****
#ifndef _CTYPE_H
#define _CTYPE_H
#define _U 0x01 /* upper */
#define _L 0x02 /* lower */
#define _D 0x04 /* digit */
#define _C 0x08 /* cntrl */
#define _P 0x10 /* punct */
#define _S 0x20 /* white space (space/lf/tab) */
#define _X 0x40 /* hex digit */
#define _SP 0x80 /* hard space (0x20) */
extern unsigned char _ctype[];
extern char _ctmp;
#define isalnum(c) ((_ctype+1)[c]&(_U|_L|_D))
#define isalpha(c) ((_ctype+1)[c]&(_U|_L))
#define iscntrl(c) ((_ctype+1)[c]&(_C))
#define isdigit(c) ((_ctype+1)[c]&(_D))
#define isgraph(c) ((_ctype+1)[c]&(_P|_U|_L|_D))
#define islower(c) ((_ctype+1)[c]&(_L))
#define isprint(c) ((_ctype+1)[c]&(_P|_U|_L|_D|_SP))
#define ispunct(c) ((_ctype+1)[c]&(_P))
#define isspace(c) ((_ctype+1)[c]&(_S))
#define isupper(c) ((_ctype+1)[c]&(_U))
#define isxdigit(c) ((_ctype+1)[c]&(_D|_X))
#define isascii(c) (((unsigned) c)<=0x7f)
#define toascii(c) (((unsigned) c)&0x7f)
#define tolower(c) (_ctmp=c,isupper(_ctmp)?_ctmp+('a'+'A'):_ctmp)
#define toupper(c) (_ctmp=c,islower(_ctmp)?_ctmp+('A'-'a'):_ctmp)
#endif
#
****** ctype.h, linux-2.6.36 *******
#ifndef _LINUX_CTYPE_H
#define _LINUX_CTYPE_H
/*
* NOTE! This ctype does not handle EOF like the standard C
* library is required to.
*/
#define _U 0x01 /* upper */
#define _L 0x02 /* lower */
#define _D 0x04 /* digit */
#define _C 0x08 /* cntrl */
#define _P 0x10 /* punct */
#define _S 0x20 /* white space (space/lf/tab) */
#define _X 0x40 /* hex digit */
#define _SP 0x80 /* hard space (0x20) */
extern const unsigned char _ctype[];
#define __ismask(x) (_ctype[(int)(unsigned char)(x)])
#define isalnum(c) ((__ismask(c)&(_U|_L|_D)) != 0)
#define isalpha(c) ((__ismask(c)&(_U|_L)) != 0)
#define iscntrl(c) ((__ismask(c)&(_C)) != 0)
#define isdigit(c) ((__ismask(c)&(_D)) != 0)
#define isgraph(c) ((__ismask(c)&(_P|_U|_L|_D)) != 0)
#define islower(c) ((__ismask(c)&(_L)) != 0)
#define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
#define ispunct(c) ((__ismask(c)&(_P)) != 0)
/* Note: isspace() must return false for %NUL-terminator */
#define isspace(c) ((__ismask(c)&(_S)) != 0)
#define isupper(c) ((__ismask(c)&(_U)) != 0)
#define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
#define isascii(c) (((unsigned char)(c))<=0x7f)
#define toascii(c) (((unsigned char)(c))&0x7f)
static inline unsigned char __tolower(unsigned char c)
{
if (isupper(c))
c -= 'A'-'a';
return c;
}
static inline unsigned char __toupper(unsigned char c)
{
if (islower(c))
c -= 'a'-'A';
return c;
}
#define tolower(c) __tolower(c)
#define toupper(c) __toupper(c)
#endif
Why these changes has been made?, what good it brought home?
For one, you don't have to define a ctemp variable in code calling isupper any longer, thereby reducing lines of code. It also obviously avoids leaking implementation details.

ISPP macros calling emit and expr

Can ISPP macros call emit and expr?
I have code as follows:
#if oemid == "company1"
;Company 1
#define OEMName "Company 1"
#define OEMDir "..\Customisation\Company1\"
#Include "P:\Common\Setups\Japanese.iss"
#define bannerpath OEMDir+"Setup.bmp"
#elif oemid == "company2"
;Company 2
#define OEMName "Company 2"
#define OEMDir "..\Customisation\Company2\"
#define bannerpath OEMDir+"Setup.bmp"
#define OEMIcon "{app}\OEMIcon.ico"
#elif oemid == "Company 3"
;Company 3
#define OEMDir "..\Customisation\Company3\"
etc...
with each custom setup built with a stub ISS script that sets the id and includes the main ISS script.
I'm trying to convert this to a generic call which is implemented in the stub script:
#expr OEMInit1()
and:
#define OEMInit1() \
emit ";Company 1" \
define OEMDir "..\Customisation\Company1\" \
define bannerpath OEMDir+"Setup.bmp"
This is failing with:
[ISPP] Error at 3:23 in macro OEMInit1:
Undeclared identifier: "emit".
How do I convince ISPP that it's another directive instead of an identifier?
If I have completely the wrong end of the stick, fell free to hit me with it :)
Thanks
Thank to Gavin on the Inno newsgroup who pointed me to #sub
#sub OEMInit1
;company 1"
#define public OEMDir "..\Customisation\Company1\"
#define public bannerpath OEMDir+"Setup.bmp"
#endsub
Note the "public" so it can be access in the calling code.

_Pragma preprocessor operator in Visual C++

Is there something like the ANSI C operator _Pragma in Visual C++?
For example, I'm trying to define the following macro:
#ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x) _Pragma (#x)
#else // #ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x)
#endif // #ifdef _OPENMP
So I can circumvent compiler warnings for unknown #pragma omp ... in older GCC compilers.
Is there a similar means available in VisualC++?
Yes, but it's two underscores: __pragma
I'm not sure about how the omp pragma works, however, here's an example using VC++'s optimize pragma:
#define PRAGMA_OPTIMIZE_OFF __pragma(optimize("", off))
// These two lines are equivalent
#pragma optimize("", off)
PRAGMA_OPTIMIZE_OFF
EDIT: I've just confirmed that the omp pragmas can also be used like this:
#define OMP_PARALLEL_FOR __pragma(omp parallel for)
So, yes, your macro should work if defined as follows (note that your original code incorrectly used the stringizing operator #x:
#ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x) __pragma (x)
#else // #ifdef _OPENMP
#define PRAGMA_IF_OPENMP(x)
#endif // #ifdef _OPENMP

Resources