lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sun, 08 Oct 2023 16:49:06 +0900 (JST)
From: FUJITA Tomonori <fujita.tomonori@...il.com>
To: tmgross@...ch.edu
Cc: fujita.tomonori@...il.com, netdev@...r.kernel.org,
 rust-for-linux@...r.kernel.org, andrew@...n.ch,
 miguel.ojeda.sandonis@...il.com, greg@...ah.com
Subject: Re: [PATCH v2 1/3] rust: core abstractions for network PHY drivers

On Sun, 8 Oct 2023 02:19:46 -0400
Trevor Gross <tmgross@...ch.edu> wrote:

> On Sat, Oct 7, 2023 at 6:33 PM FUJITA Tomonori
> <fujita.tomonori@...il.com> wrote:
>> To create an internal type based on `name`, we need to unstringify
>> `name`? I can't find a easy way to do it.
> 
> I think you should just be able to do it with `paste!`
> 
>     macro_rules! module_phy_driver {
>         (name: $name:expr) => {
>             paste::paste! {
>                 #[allow(non_camel_case_types)]
>                 struct [<$name _ty>];
>             }
>         }
>     }
> 
>     // creates struct `demo_driver_ty`
>     module_phy_driver! {
>         name: "demo_driver"
>     }
> 

I realized that we don't need `name`. The name of struct doesn't
matter so I use `Module`. I tried to use `name` for the name of
device_table however the variable name of the table isn't embeded into
the module binary so it doesn't matter.

FYI, I use paste! but got the following error:

= help: message: `"__mod_mdio__\"rust_asix_phy\"_device_table"` is not a valid identifier
= note: this error originates in the macro `$crate::module_phy_driver` which comes from the expansion of the
  macro `kernel::module_phy_driver` (in Nightly builds, run with -Z macro-backtrace for more info)


#[macro_export]
macro_rules! module_phy_driver {
    (@replace_expr $_t:tt $sub:expr) => {$sub};

    (@count_devices $($x:expr),*) => {
        0usize $(+ $crate::module_phy_driver!(@replace_expr $x 1usize))*
    };

    (@device_table $name:tt, [$($dev:expr),+]) => {
        ::kernel::macros::paste! {
            #[no_mangle]
            static [<__mod_mdio__ $name _device_table>]: [
                kernel::bindings::mdio_device_id;
                $crate::module_phy_driver!(@count_devices $($dev),+) + 1
            ] = [
                $(kernel::bindings::mdio_device_id {
                    phy_id: $dev.id,
                    phy_id_mask: $dev.mask_as_int()
                }),+,
                kernel::bindings::mdio_device_id {
                    phy_id: 0,
                    phy_id_mask: 0
                }
            ];
        }
    };

    (drivers: [$($driver:ident),+], device_table: [$($dev:expr),+], name: $name:tt, $($f:tt)*) => {
        struct Module {
            _reg: kernel::net::phy::Registration,
        }

        $crate::prelude::module! {
             type: Module,
             name: $name,
             $($f)*
        }

        static mut DRIVERS: [
            kernel::types::Opaque<kernel::bindings::phy_driver>;
            $crate::module_phy_driver!(@count_devices $($driver),+)
        ] = [
            $(kernel::net::phy::create_phy_driver::<$driver>()),+
        ];

        impl kernel::Module for Module {
            fn init(module: &'static ThisModule) -> Result<Self> {
                // SAFETY: static `DRIVERS` array is used only in the C side.
                let mut reg = unsafe { kernel::net::phy::Registration::register(module, &DRIVERS) }?;

                Ok(Module {
                    _reg: reg,
                })
            }
        }

        $crate::module_phy_driver!(@device_table $name, [$($dev),+]);
    }
}

Powered by blists - more mailing lists