Sylixos Hot Swap System

Source: Internet
Author: User
Tags signal handler
1. Introduction

The so-called Hot plug (hot-pluggin) is charged Plug and unplug, hot plug function is to allow the user to not shut down the system, without cutting off the power of the insertion or dial-out device, such as: USB device (USB stick, USB mouse, etc.) SD card device. This seemingly simple operation on the user, at the system level has a complex processing. 2. Introduction to Sylixos Hot swap system

Early in the computer system, programmers knew they needed to scan all the devices at startup, and they never had to worry about the device disappearing until the whole machine was shut down. Now, with the advent of USB devices, CardBus, PCI hotplug controllers, the Sylixos system needs to be able to safely and reliably add or remove hardware from the system, which is undoubtedly an additional burden to the driver designers, because now they have to deal with a device that pops up or suddenly disappears.

For the above situation, many operating systems have added support for hot plug-in, such as: Linux, Windows, etc., of course, Sylixos also added a set of its own hot plug mechanism.

In two cases Sylixos hot plug, one is the kernel of hot plug is the hardware, that is, the interface between the kernel and the device driver, the other is the user view hot plug is "/dev/hotplug" device files, hot plug device files are created by the kernel. 3. Sylixos hot-swap system principle Analysis 3.1 Sylixos detect hot-swappable devices Method Hot-plug events, such as: USB devices can be plugged in and unplugged by Api_ The Hotplugevent function handles both events asynchronously. In an event handler, the driver can use Oemdiskmount or other mount functions to mount a large-capacity device. Or create other devices on your own; cyclic detection, when some hot-swappable devices cannot produce events, (ex: devices without plug-in interrupts) need polling to detect certain event flags to get the state of the device, then the driver does not need to create the thread itself or use the timer to check, just call Api_ The Hotplugpolladd function inserts the detection function and parameters into the HotPlug loop detection queue, t_hotplug the thread invokes the detection function periodically. Hot-swappable messages, when the device hot-plug operation ends, can be api_ Hotplugeventmessage notifies the application of hot-swappable results, and the application can get hot-swappable messages for driver notifications by reading "/dev/hotplug" files. analysis of 3.2 Sylixos hot-swap system

We said above the hot plug in two cases, one is the system and the driver, the second is the system and the user layer. 3.2.1 System and drive layer

The interaction between the system and the drive layer, we are divided into hot plug events and loop check. Hot-Plug events are for devices capable of generating hot-plug interrupt notifications, so that Sylixos can handle this notification asynchronously, while a loop check is that the device does not produce a hot plug interrupt notification and requires polling to detect certain event flags to get the device's insertion and removal status. 1. Hot Swap event:

A device capable of generating hot-swappable events, in the driver layer as long as the function api_hotplugevent can be called, Sylixos hot-swap system in the hot-swap event processing is through our previous blog post said Jobqueue to achieve, here we no longer do more explanation. 2. Cyclic detection:

For a device that cannot generate a hot-plug interrupt event, the driver layer can call the function Api_hotplugpolladd to add the polling check function in the driver to the system so that the system polls the device state at a certain interval of time, and here we introduce the principle of this method.

typedef struct {
    lw_list_line    hppn_linemanage;               /*  Node link list                    */
    voidfuncptr     hppn_pfunc;                    /*  function pointer                    */
    PVOID           hppn_pvarg;                    /*  function parameter                    */
    lw_resource_raw Hppn_resraw;                   /*  Resource Management node                */
} Lw_hotplug_pollnode;
Lw_list_line_header   _g_plinehotplugpoll = lw_null;/*  hot-swappable cycle detection chain            */

The above structure is polling the detection node, function pointers and function parameters two members are the driver designer's concern, that is, the driver polling detection function, from the following code fragment we can confirm this, the global link header _g_plinehotplugpoll is linked to all hot plug jointing points.

Lw_api  
INT  api_hotplugpolladd (voidfuncptr   pfunc, PVOID  pvarg)
{
    ...
    Phppn->hppn_pfunc = Pfunc;
    Phppn->hppn_pvarg = Pvarg;
    ...

    Return  (Error_none);
}

The Sylixos system layer loops through the "T_hotplug" thread to check the hot plug jointing point chain, which we can confirm from the code snippet below.

static void  _hotplugthread (void)
{
    ULONG  ulerror;

    for (;;) {
        ...
            for (plinetemp  = _g_plinehotplugpoll;
                 Plinetemp! = Lw_null;
                 Plinetemp  = _list_line_get_next (plinetemp)) {

                phppn = _list_entry (Plinetemp, Lw_hotplug_pollnode, HPPN_ Linemanage);
                if (phppn->hppn_pfunc) {
                    /
                     * *  calls the previously installed loop detection function *
                    /Phppn->hppn_pfunc (phppn->hppn_ PVARG);
                }
            }
        ...
        }
    }
}
3.2.2 System and User layer

The interaction between the system and the user layer is that the system will notify the user layer of hot-swappable messages, so that the user layer can perceive the insertion and removal of the device, api_hotplugeventmessage the hot-plug message by calling the function, and then the user layer reads the device "/dev/hotplug" To get hot-swappable messages.

Here the implementation of the principle is divided into two parts, one is the hot Plug device part, the second is the message delivery part: 1. Hot-swappable device parts:

The creation of hot-swappable devices involves the knowledge of sylixos device drivers, which are not detailed here and will be described in detail later in the article. 2. Message Delivery section:

The message passing section here uses the previously blog post block message, as seen in the code snippet below.

LW_API int api_hotplugeventmessage (int imsg, BOOL binsert,
                                       Cpchar Pcpath, UINT32 uiArg0,
                                       UINT32 UiArg1, UINT32 uiArg2,

    UINT32 uiArg3) {... _hotplugdevputmsg (imsg, Ucbuffer, i);
return (Error_none); } VOID _hotplugdevputmsg (INT imsg, cpvoid pvmsg, size_t stsize) {... for (plinetemp = _g_hotplugdev.
         Hotpdev_plinefile;
         Plinetemp! = Lw_null; Plinetemp = _list_line_get_next (plinetemp)) {Photplugfil = _list_entry (Plinetemp, Lw_hotplug_file, HOTPFIL_line
        Manage);
            if ((photplugfil->hotpfil_imsg = = imsg) | | (photplugfil->hotpfil_imsg = = Lw_hotplug_msg_all))
            {_bmsgput (photplugfil->hotpfil_pbmsg, pvmsg, stsize); Api_semaphoreBpost (Photplugfil->hotpfil_ulreadsync);
        Bwakeup = Lw_true;
 }
    }
    ...
}
4. Application examples

The following program through the signal to imitate the hardware interrupt to achieve the hot plug event mechanism, the program uses the signal SIGALRM imitate the device to insert, with the signal SIGUSR1 imitate the device to pull out, this example is only to show the use of hot-swap function.

#define __sylixos_kernel #include <SylixOS.h> #include <stdio.h> #include <string.h> #include <
signal.h> #include <pthread.h> #include <unistd.h> static char *msg = "Dev";
Static pthread_t Tid;

static int stop = 0;
    void *process_thread (void *arg) {int *FD = (int *) arg;

    Char buf[64];
        while (1) {memset (buf, 0, 64);

        Read (*FD, buf, 64);
        if (buf[4] = = 1) {fprintf (stdout, "%s insert.\n", buf + 5);
        } else {fprintf (stdout, "%s pullout.\n", buf + 5);
        } if (stop) {pthread_exit (NULL);
}} return NULL;

    } void Send_event (void *arg) {int signum = (int) arg;
    if (Signum = = sigalrm) {api_hotplugeventmessage (Lw_hotplug_msg_all, 1, MSG, 0, 0, 0, 0);
    } else if (Signum = = SIGUSR1) {api_hotplugeventmessage (lw_hotplug_msg_all, 0, MSG, 0, 0, 0, 0); }} void Pullout_handler (int signum) {Api_hotPlugevent ((voidfuncptr) send_event, (void *) Signum, 0, 0, 0, 0, 0);

} void Insert_handler (int signum) {api_hotplugevent ((voidfuncptr) send_event, (void *) Signum, 0, 0, 0, 0, 0);}
    int main (int argc, char *argv[]) {int FD;

    int ret, I;
    FD = open ("/dev/hotplug", o_rdonly);
        if (FD < 0) {fprintf (stderr, "Open hotplug dev failed.\n");
    return-1;
    } ret = Pthread_create (&tid, NULL, Process_thread, (void *) &AMP;FD);
        if (Ret < 0) {fprintf (stderr, "Create thread failed.\n");
    return-1;
        } if (Signal (SIGALRM, insert_handler) = = Sig_err) {fprintf (stderr, "Install signal Handler failed.\n");
    return-1;
        } if (Signal (SIGUSR1, pullout_handler) = = Sig_err) {fprintf (stderr, "Install signal Handler failed.\n");
    return-1;
        } for (i = 0; i < ++i) {alarm (2);
        Pause ();
    Pthread_kill (Getpid (), SIGUSR1);
    } stop = 1; Pthread_Join (TID, NULL);
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.