Re: Problem with a UDP server implements

2011-10-19 Thread Bernd Petrovitsch
Hi!

On Die, 2011-10-18 at 22:48 +0800, tao jiang wrote:
[...]
 Thanks for your guide.
 But there is still a question
 if recvfrom() used, how to figure out the client's dest ip for reply ?
 i.e. on which ip the server receieved the request
 when construct the IP header, i don't know which saddr to fill in

Ooops, forgot that we need the destination address of the received
packet too to construct the answer for it.
Then yes, it seems that you would need the raw packet on the receiving
side too.

[ Full quote deelted ]

Bernd
-- 
Bernd Petrovitsch  Email : be...@petrovitsch.priv.at
 LUGA : http://www.luga.at


___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


print contents of runqueue

2011-10-19 Thread kashish bhatia
Hi,

I want to print contents of runqueue of my current processor.
I got the pointer to current task_struct.

To get runqueue, I called task_rq().
When compiled the module, it gave an error.

Actually struct runqueue and task_rq() is not in any header file. It is
present in kernel/sched.c

Is there any way to access this structure without making changes to kernel .
-- 
Regards,
Kashish
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Trouble removing character device

2011-10-19 Thread Kai Meyer
I can't seem to get my character device to remove itself from the 
/proc/devices list. I'm calling all of the following functions like so:

alloc_chrdev_region(dev, 0, 5, my_char);
cdev_init(my_cdev, my_ops);
cdev_add(my_cdev, MKDEV(my_major, my_minor), 1);
cdev_del(my_cdev);
unregister_chrdev_region(my_major, 5);

It seems like I'm missing something, but I can't find it. I'm 
referencing the Linux Device Drivers v3, chapter 3. In the example code, 
the scull_cleanup_module function calls cdev_dell and 
unregister_chrdev_region, just like I do.

To be clear, after I unload my module (after calling cdev_del and 
unregister_chrdev_region), my my_char string still shows up in 
/proc/devices.

-Kai Meyer

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: mmap local APIC address

2011-10-19 Thread Vaibhav Jain
On Fri, Oct 14, 2011 at 9:10 PM, Mulyadi Santosa
mulyadi.sant...@gmail.comwrote:

 hi...

 On Sat, Oct 15, 2011 at 07:22, Vaibhav Jain vjoss...@gmail.com wrote:
  Hi,
 
  I am trying to map local APIC on an intel system (physical address
  0xfee0) in a user level program using mmap
  but my mmap is failing saying Bad file descriptor. I am not sure if
 this
  problem is related to apic or
  mmap. I am running the program as root.  Please help me figure this out.

 could you post the code?

 anyway, check man mmap in errors sectionthe hint could be
 found there.

 --
 regards,

 Mulyadi Santosa
 Freelance Linux trainer and consultant

 blog: the-hydra.blogspot.com
 training: mulyaditraining.blogspot.com



Hi,

Here's the code



typedef unsigned int u32;

#define MAP_LEN 0x1000  //4 KB page
#define BUFLEN 100
*#define APIC_BASE   0xfee0*
#define APIC_ICR_low0xfee00300
#define APIC_ICR_high   0xfee00310
#define APIC_ICR_init   0x0500
#define APIC_ICR_start  0x0600
#define APIC_ICR_ASSERT_LVL_TRG 0xc000
#define BOUNCE_CODE0x2000

int apic_reset_cpu(cpu);
int cpu = 2;

int main(){
if ((apic_reset_cpu(cpu)) != 0)
printf(INIT  Startup failed!\n);
}


int apic_reset_cpu(int cpu)
{
  unsigned long tmpaddress, apic_icr, *apic_phy_addr;
  int fd, sleep_now=0;


  printf(Assuming APIC physical base: %lx \n, APIC_BASE);
  tmpaddress = (unsigned long) mmap(NULL, MAP_LEN, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, (off_t)APIC_BASE);

*  if(tmpaddress == (unsigned long)MAP_FAILED) //check if it worked
  {
perror(Mapping memory for absolute memory access failed.\n);
return -3;
  }*

  //prep ICR high
  apic_icr = tmpaddress;
  apic_icr |= APIC_ICR_high;
  apic_phy_addr = (unsigned long *) apic_icr;
  *apic_phy_addr = cpu  24;

  //prep ICR low Send INIT and wait
  apic_icr = tmpaddress;
  apic_icr |= APIC_ICR_low;
  apic_phy_addr = (unsigned long *) apic_icr;
  *apic_phy_addr = APIC_ICR_init | APIC_ICR_ASSERT_LVL_TRG;
  sleep_now = usleep( 1 ); //sleep 10 ms

  //prep for SIPI
  apic_icr = tmpaddress;
  apic_icr |= APIC_ICR_low;
  apic_phy_addr = (unsigned long *) apic_icr;
  *apic_phy_addr = APIC_ICR_start | (BOUNCE_CODE  12);
  sleep_now = usleep( 500 ); //sleep 500 usec

  printf(Unmapping APIC Base page\n);
  tmpaddress = 0xf000;
  munmap(tmpaddress, MAP_LEN);

  return 0;
}

Thanks
Vaibhav Jain
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: mmap local APIC address

2011-10-19 Thread Vaibhav Jain
Hi,

Please ignore the email below. I am sorry I  made a very silly mistake.

Thanks
Vaibhav Jain

On Wed, Oct 19, 2011 at 10:12 AM, Vaibhav Jain vjoss...@gmail.com wrote:

 On Fri, Oct 14, 2011 at 9:10 PM, Mulyadi Santosa 
 mulyadi.sant...@gmail.com wrote:

 hi...

 On Sat, Oct 15, 2011 at 07:22, Vaibhav Jain vjoss...@gmail.com wrote:
  Hi,
 
  I am trying to map local APIC on an intel system (physical address
  0xfee0) in a user level program using mmap
  but my mmap is failing saying Bad file descriptor. I am not sure if
 this
  problem is related to apic or
  mmap. I am running the program as root.  Please help me figure this out.

 could you post the code?

 anyway, check man mmap in errors sectionthe hint could be
 found there.

 --
 regards,

 Mulyadi Santosa
 Freelance Linux trainer and consultant

 blog: the-hydra.blogspot.com
 training: mulyaditraining.blogspot.com



 Hi,

 Here's the code



 typedef unsigned int u32;

 #define MAP_LEN 0x1000  //4 KB page
 #define BUFLEN 100
 *#define APIC_BASE   0xfee0*
 #define APIC_ICR_low0xfee00300
 #define APIC_ICR_high   0xfee00310
 #define APIC_ICR_init   0x0500
 #define APIC_ICR_start  0x0600
 #define APIC_ICR_ASSERT_LVL_TRG 0xc000
 #define BOUNCE_CODE0x2000

 int apic_reset_cpu(cpu);
 int cpu = 2;

 int main(){
 if ((apic_reset_cpu(cpu)) != 0)
 printf(INIT  Startup failed!\n);
 }


 int apic_reset_cpu(int cpu)
 {
   unsigned long tmpaddress, apic_icr, *apic_phy_addr;
   int fd, sleep_now=0;


   printf(Assuming APIC physical base: %lx \n, APIC_BASE);
   tmpaddress = (unsigned long) mmap(NULL, MAP_LEN, PROT_READ|PROT_WRITE,
 MAP_SHARED, fd, (off_t)APIC_BASE);

 *  if(tmpaddress == (unsigned long)MAP_FAILED) //check if it worked
   {
 perror(Mapping memory for absolute memory access failed.\n);
 return -3;
   }*

   //prep ICR high
   apic_icr = tmpaddress;
   apic_icr |= APIC_ICR_high;
   apic_phy_addr = (unsigned long *) apic_icr;
   *apic_phy_addr = cpu  24;

   //prep ICR low Send INIT and wait
   apic_icr = tmpaddress;
   apic_icr |= APIC_ICR_low;
   apic_phy_addr = (unsigned long *) apic_icr;
   *apic_phy_addr = APIC_ICR_init | APIC_ICR_ASSERT_LVL_TRG;
   sleep_now = usleep( 1 ); //sleep 10 ms

   //prep for SIPI
   apic_icr = tmpaddress;
   apic_icr |= APIC_ICR_low;
   apic_phy_addr = (unsigned long *) apic_icr;
   *apic_phy_addr = APIC_ICR_start | (BOUNCE_CODE  12);
   sleep_now = usleep( 500 ); //sleep 500 usec

   printf(Unmapping APIC Base page\n);
   tmpaddress = 0xf000;
   munmap(tmpaddress, MAP_LEN);

   return 0;
 }

 Thanks
 Vaibhav Jain

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: mmap local APIC address

2011-10-19 Thread Vaibhav Jain
On Wed, Oct 19, 2011 at 10:57 AM, bob ilikepie...@live.com wrote:

 It would be nice to share your solution in case someone else comes into the
 same problem.

Hi,

It was stupid mistake. I forgot to initialize the file descriptor which was
passed to  mmap and therefore it was throwing Bad file descriptor error.i
realized it after looking the errors in man mmap.

Thanks
Vaibhav Jain
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Trouble removing character device

2011-10-19 Thread Daniel Baluta
On Wed, Oct 19, 2011 at 7:04 PM, Kai Meyer k...@gnukai.com wrote:
 I can't seem to get my character device to remove itself from the
 /proc/devices list. I'm calling all of the following functions like so:

 alloc_chrdev_region(dev, 0, 5, my_char);
 cdev_init(my_cdev, my_ops);
 cdev_add(my_cdev, MKDEV(my_major, my_minor), 1);
 cdev_del(my_cdev);
 unregister_chrdev_region(my_major, 5);

 It seems like I'm missing something, but I can't find it. I'm
 referencing the Linux Device Drivers v3, chapter 3. In the example code,
 the scull_cleanup_module function calls cdev_dell and
 unregister_chrdev_region, just like I do.

 To be clear, after I unload my module (after calling cdev_del and
 unregister_chrdev_region), my my_char string still shows up in
 /proc/devices.

Did you check  return codes for all functions?
Also, can you post a link to the code?

thanks,
Daniel.

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Understanding memcpy

2011-10-19 Thread Kai Meyer
I'm trying to poke around an ext4 file system. I can submit a bio for 
the correct block, and read in what seems to be the correct information, 
but when I try to memcpy my char *buffer to a reference to a struct I've 
made, it just doesn't seem to work. The relevant code looks like this:

typedef struct ext2_superblock {
 /* 00-03 */ uint32_t e2sb_inode_count;
 /* 04-07 */ uint32_t e2sb_block_count;
 /* 08-11 */ uint32_t e2sb_blocks_reserved;
 /* 12-15 */ uint32_t e2sb_unallocated_blocks;
 /* 16-19 */ uint32_t e2sb_unallocated_inodes;
 /* 20-23 */ uint32_t e2sb_sb_block;
 /* 24-27 */ uint32_t e2sb_log_block_size;
 /* 28-31 */ uint32_t e2sb_log_fragment_size;
 /* 32-35 */ uint32_t e2sb_num_blocks_per_group;
 /* 36-39 */ uint32_t e2sb_num_frag_per_group;
 /* 40-43 */ uint32_t e2sb_num_inodes_per_group;
 /* 44-47 */ uint32_t e2sb_last_mount_time;
 /* 48-51 */ uint32_t e2sb_last_written_time;
 /* 52-53 */ uint16_t e2sb_num_mounted;
 /* 54-55 */ uint16_t e2sb_num_allowed_mounts;
 /* 56-57 */ uint16_t e2sb_signature;
 /* 58-59 */ uint16_t e2sb_fs_state;
 /* 60-61 */ uint16_t e2sb_error_action;
 /* 62-63 */ uint16_t e2sb_ver_minor;
 /* 64-67 */ uint32_t e2sb_last_check;
 /* 68-71 */ uint32_t e2sb_time_between_checks;
 /* 72-75 */ uint32_t e2sb_os_id;
 /* 76-79 */ uint32_t e2sb_ver_major;
 /* 80-81 */ uint16_t e2sb_uid;
 /* 82-83 */ uint16_t e2sb_gid;
} e2sb;


char *buffer;
uint32_t *pointer;
e2sb sb;
buffer = __bio_kmap_atomic(bio, 0, KM_USER0);
pointer = (uint32_t *)buffer;
printk(KERN_DEBUG sizeof pbd-sb %lu\n, sizeof(bpd-sb));
printk(KERN_DEBUG Inode Count: %u\n, pointer[0]); /* Works! */
printk(KERN_DEBUG Block Count: %u\n, pointer[1]); /* Works! */
printk(KERN_DEBUG Block Reserved: %u\n, pointer[2]); /* Works! */
printk(KERN_DEBUG Unallocated blocks: %u\n, pointer[3]); /* Works! */
printk(KERN_DEBUG Unallocated inodes: %u\n, pointer[4]); /* Works! */
memcpy(buffer, sb, sizeof(sb));
__bio_kunmap_atomic(bio, KM_USER0);
printk(KERN_DEBUG e2sb_debug: Total number of inodes in file system 
%u\n, sb-e2sb_inode_count);/* Doesn't work! */
printk(KERN_DEBUG e2sb_debug: Total number of blocks in file 
system%u\n, sb-e2sb_block_count); /* Doesn't work! */

My code is actually much more verbose. The values I get from indexing 
into pointer are correct, and match what I get from dumpe2fs. The values 
I get from the e2sb struct are not. They are usually 0. I would imagine 
that memcpy is the fastest way to copy data from buffer instead of 
casting the pointer to something else, and using array indexing to get 
the values.

I struggled to find where ext4 actually does this, so I'm making this up 
as I go along. Any thing that you see that I should be doing a different 
way that isn't actually part of my question is welcome too.

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Trouble removing character device

2011-10-19 Thread rohan puri
On Thu, Oct 20, 2011 at 2:25 AM, Kai Meyer k...@gnukai.com wrote:

 Unfortunately I can't share the source code, it belongs to the company I
 work for.

 All of cdev_init, cdev_del, and unregister_chrdev_region are void
 functions, so they have no return value.

 I check the return value of alloc_chrdev_region and cdev_add and check
 for errors with BUG_ON (for now).

 -Kai Meyer

 On 10/19/2011 02:18 PM, Daniel Baluta wrote:
  On Wed, Oct 19, 2011 at 7:04 PM, Kai Meyerk...@gnukai.com  wrote:
  I can't seem to get my character device to remove itself from the
  /proc/devices list. I'm calling all of the following functions like so:
 
  alloc_chrdev_region(dev, 0, 5, my_char);
  cdev_init(my_cdev,my_ops);
  cdev_add(my_cdev, MKDEV(my_major, my_minor), 1);
  cdev_del(my_cdev);
  unregister_chrdev_region(my_major, 5);
 
  It seems like I'm missing something, but I can't find it. I'm
  referencing the Linux Device Drivers v3, chapter 3. In the example code,
  the scull_cleanup_module function calls cdev_dell and
  unregister_chrdev_region, just like I do.
 
  To be clear, after I unload my module (after calling cdev_del and
  unregister_chrdev_region), my my_char string still shows up in
  /proc/devices.
  Did you check  return codes for all functions?
  Also, can you post a link to the code?
 
  thanks,
  Daniel.
 
  ___
  Kernelnewbies mailing list
  Kernelnewbies@kernelnewbies.org
  http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

 ___
 Kernelnewbies mailing list
 Kernelnewbies@kernelnewbies.org
 http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


During cleanup i think you need to call function unregister_chrdev_region().

Regards,
Rohan Puri
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Understanding memcpy

2011-10-19 Thread rohan puri
On Thu, Oct 20, 2011 at 3:04 AM, Kai Meyer k...@gnukai.com wrote:

 I'm trying to poke around an ext4 file system. I can submit a bio for
 the correct block, and read in what seems to be the correct information,
 but when I try to memcpy my char *buffer to a reference to a struct I've
 made, it just doesn't seem to work. The relevant code looks like this:

 typedef struct ext2_superblock {
 /* 00-03 */ uint32_t e2sb_inode_count;
 /* 04-07 */ uint32_t e2sb_block_count;
 /* 08-11 */ uint32_t e2sb_blocks_reserved;
 /* 12-15 */ uint32_t e2sb_unallocated_blocks;
 /* 16-19 */ uint32_t e2sb_unallocated_inodes;
 /* 20-23 */ uint32_t e2sb_sb_block;
 /* 24-27 */ uint32_t e2sb_log_block_size;
 /* 28-31 */ uint32_t e2sb_log_fragment_size;
 /* 32-35 */ uint32_t e2sb_num_blocks_per_group;
 /* 36-39 */ uint32_t e2sb_num_frag_per_group;
 /* 40-43 */ uint32_t e2sb_num_inodes_per_group;
 /* 44-47 */ uint32_t e2sb_last_mount_time;
 /* 48-51 */ uint32_t e2sb_last_written_time;
 /* 52-53 */ uint16_t e2sb_num_mounted;
 /* 54-55 */ uint16_t e2sb_num_allowed_mounts;
 /* 56-57 */ uint16_t e2sb_signature;
 /* 58-59 */ uint16_t e2sb_fs_state;
 /* 60-61 */ uint16_t e2sb_error_action;
 /* 62-63 */ uint16_t e2sb_ver_minor;
 /* 64-67 */ uint32_t e2sb_last_check;
 /* 68-71 */ uint32_t e2sb_time_between_checks;
 /* 72-75 */ uint32_t e2sb_os_id;
 /* 76-79 */ uint32_t e2sb_ver_major;
 /* 80-81 */ uint16_t e2sb_uid;
 /* 82-83 */ uint16_t e2sb_gid;
 } e2sb;


 char *buffer;
 uint32_t *pointer;
 e2sb sb;
 buffer = __bio_kmap_atomic(bio, 0, KM_USER0);
 pointer = (uint32_t *)buffer;
 printk(KERN_DEBUG sizeof pbd-sb %lu\n, sizeof(bpd-sb));
 printk(KERN_DEBUG Inode Count: %u\n, pointer[0]); /* Works! */
 printk(KERN_DEBUG Block Count: %u\n, pointer[1]); /* Works! */
 printk(KERN_DEBUG Block Reserved: %u\n, pointer[2]); /* Works! */
 printk(KERN_DEBUG Unallocated blocks: %u\n, pointer[3]); /* Works! */
 printk(KERN_DEBUG Unallocated inodes: %u\n, pointer[4]); /* Works! */
 memcpy(buffer, sb, sizeof(sb));

This should be : -
memcpy(sb, buffer, sizeof(sb));


 __bio_kunmap_atomic(bio, KM_USER0);
 printk(KERN_DEBUG e2sb_debug: Total number of inodes in file system
 %u\n, sb-e2sb_inode_count);/* Doesn't work! */
 printk(KERN_DEBUG e2sb_debug: Total number of blocks in file
 system%u\n, sb-e2sb_block_count); /* Doesn't work! */

 My code is actually much more verbose. The values I get from indexing
 into pointer are correct, and match what I get from dumpe2fs. The values
 I get from the e2sb struct are not. They are usually 0. I would imagine
 that memcpy is the fastest way to copy data from buffer instead of
 casting the pointer to something else, and using array indexing to get
 the values.

 I struggled to find where ext4 actually does this, so I'm making this up
 as I go along. Any thing that you see that I should be doing a different
 way that isn't actually part of my question is welcome too.

 ___
 Kernelnewbies mailing list
 Kernelnewbies@kernelnewbies.org
 http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Regards,
Rohan Puri
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Getting 'bad file number' error writing to device driver

2011-10-19 Thread Mandeep Sandhu
 Thanks for the link; but this bit has to be set on the original executable.
 Is there any way a program in user mode can get around this or set the bit
 himself?

Ummm...now that would be a security violation, don't you think? If a
program can elevate it's own privileges, then whats the point of
having access control! :)

I think a user-space program will always need _some_ help from a
privileged user/program. Eg in the case of setuid, typically the root
user will be setting this bit (on a executable) so that an
unprivileged user can execute with temporarily elevated privileges.

CMIIW.

HTH,
-mandeep


 Anil

 ___
 Kernelnewbies mailing list
 Kernelnewbies@kernelnewbies.org
 http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies